Skip to content

Commit

Permalink
refactor: replace static with const for constants (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech authored Dec 9, 2024
1 parent 239c55a commit 6b67223
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ impl PingChecker {
async fn check_request_tcp_chromium(&self) -> io::Result<()> {
use std::io::{Error, ErrorKind};

static GET_BODY: &[u8] =
const GET_BODY: &[u8] =
b"GET /generate_204 HTTP/1.1\r\nHost: clients3.google.com\r\nConnection: close\r\nAccept: */*\r\n\r\n";

let addr = Address::DomainNameAddress("clients3.google.com".to_owned(), 80);
Expand Down Expand Up @@ -892,7 +892,7 @@ impl PingChecker {
async fn check_request_tcp_firefox(&self) -> io::Result<()> {
use std::io::{Error, ErrorKind};

static GET_BODY: &[u8] =
const GET_BODY: &[u8] =
b"GET /success.txt HTTP/1.1\r\nHost: detectportal.firefox.com\r\nConnection: close\r\nAccept: */*\r\n\r\n";

let addr = Address::DomainNameAddress("detectportal.firefox.com".to_owned(), 80);
Expand Down Expand Up @@ -938,7 +938,7 @@ impl PingChecker {
// - QNAME: \x07 firefox \x03 com \x00
// - QTYPE: 0x0001 A
// - QCLASS: 0x0001 IN
static DNS_QUERY: &[u8] =
const DNS_QUERY: &[u8] =
b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07firefox\x03com\x00\x00\x01\x00\x01";

let addr = Address::SocketAddress(SocketAddr::new(Ipv4Addr::new(8, 8, 8, 8).into(), 53));
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/online_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl OnlineConfigService {
}

async fn run_once_impl(&mut self) -> io::Result<()> {
static SHADOWSOCKS_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
const SHADOWSOCKS_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

let start_time = Instant::now();

Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/dns_resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async fn hickory_dns_notify_update_dns(resolver: Arc<HickoryDnsSystemResolver>)

use super::hickory_dns_resolver::create_resolver;

static DNS_RESOLV_FILE_PATH: &str = "/etc/resolv.conf";
const DNS_RESOLV_FILE_PATH: &str = "/etc/resolv.conf";

if !Path::new(DNS_RESOLV_FILE_PATH).exists() {
trace!("resolv file {DNS_RESOLV_FILE_PATH} doesn't exist");
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/relay/tcprelay/aead_2022.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ pub struct EncryptedWriter {
impl EncryptedWriter {
/// Creates a new EncryptedWriter
pub fn new(stream_ty: StreamType, method: CipherKind, key: &[u8], nonce: &[u8]) -> EncryptedWriter {
static EMPTY_IDENTITY: [Bytes; 0] = [];
const EMPTY_IDENTITY: [Bytes; 0] = [];
EncryptedWriter::with_identity(stream_ty, method, key, nonce, &EMPTY_IDENTITY)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/relay/tcprelay/crypto_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl<S> CryptoStream<S> {
method: CipherKind,
key: &[u8],
) -> CryptoStream<S> {
static EMPTY_IDENTITY: [Bytes; 0] = [];
const EMPTY_IDENTITY: [Bytes; 0] = [];
CryptoStream::from_stream_with_identity(context, stream, stream_ty, method, key, &EMPTY_IDENTITY, None)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<S> ProxyServerStream<S> {
#[cfg(not(feature = "aead-cipher-2022"))]
let writer_state = ProxyServerStreamWriteState::Established;

static EMPTY_IDENTITY: [Bytes; 0] = [];
const EMPTY_IDENTITY: [Bytes; 0] = [];
ProxyServerStream {
stream: CryptoStream::from_stream_with_identity(
&context,
Expand Down
4 changes: 2 additions & 2 deletions crates/shadowsocks/tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn tcp_tunnel_example(

let mut client = TcpStream::connect(local_addr).await?;

static HTTP_REQUEST: &[u8] = b"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\nConnection: close\r\n\r\n";
const HTTP_REQUEST: &[u8] = b"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\nConnection: close\r\n\r\n";
client.write_all(HTTP_REQUEST).await?;

let mut reader = BufReader::new(client);
Expand All @@ -145,7 +145,7 @@ async fn tcp_tunnel_example(

println!("{:?}", ByteStr::new(&buffer));

static HTTP_RESPONSE_STATUS: &[u8] = b"HTTP/1.0 200 OK\r\n";
const HTTP_RESPONSE_STATUS: &[u8] = b"HTTP/1.0 200 OK\r\n";
assert!(buffer.starts_with(HTTP_RESPONSE_STATUS));

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/tests/tcp_tfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ async fn tcp_tunnel_tfo() {

println!("{:?}", ByteStr::new(&buffer));

static HTTP_RESPONSE_STATUS: &[u8] = b"HTTP/1.0 200 OK\r\n";
const HTTP_RESPONSE_STATUS: &[u8] = b"HTTP/1.0 200 OK\r\n";
assert!(buffer.starts_with(HTTP_RESPONSE_STATUS));
}
2 changes: 1 addition & 1 deletion crates/shadowsocks/tests/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async fn udp_tunnel_echo(
let socket = UdpSocket::bind("0.0.0.0:0").await?;
socket.connect(local_addr).await?;

static SEND_PAYLOAD: &[u8] = b"HELLO WORLD. \x0012345";
const SEND_PAYLOAD: &[u8] = b"HELLO WORLD. \x0012345";
socket.send(SEND_PAYLOAD).await?;

let mut buffer = [0u8; 65536];
Expand Down
2 changes: 1 addition & 1 deletion tests/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn dns_relay() {
time::sleep(Duration::from_secs(1)).await;

// Query firefox.com, TransactionID: 0x1234
static DNS_QUERY: &[u8] = b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07firefox\x03com\x00\x00\x01\x00\x01";
const DNS_QUERY: &[u8] = b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07firefox\x03com\x00\x00\x01\x00\x01";

// 1. DoT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/socks4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn socks4_relay_connect() {
let svr = Socks4TestServer::new(SERVER_ADDR, LOCAL_ADDR, PASSWORD, METHOD);
svr.run().await;

static HTTP_REQUEST: &[u8] = b"GET /success.txt HTTP/1.0\r\nHost: detectportal.firefox.com\r\nAccept: */*\r\n\r\n";
const HTTP_REQUEST: &[u8] = b"GET /success.txt HTTP/1.0\r\nHost: detectportal.firefox.com\r\nAccept: */*\r\n\r\n";

let mut c = Socks4TcpClient::connect(("detectportal.firefox.com", 80), LOCAL_ADDR, Vec::new())
.await
Expand Down

0 comments on commit 6b67223

Please sign in to comment.