diff --git a/api/api.go b/api/api.go index 2f803d6..e93975a 100644 --- a/api/api.go +++ b/api/api.go @@ -9,7 +9,7 @@ import ( type Handler func(ctx context.Context, auth statistic.Authenticator) error -var handlers = map[string]Handler{} +var handlers = make(map[string]Handler) func RegisterHandler(name string, handler Handler) { handlers[name] = handler diff --git a/api/service/server_test.go b/api/service/server_test.go index 25bab93..6fa786d 100644 --- a/api/service/server_test.go +++ b/api/service/server_test.go @@ -310,8 +310,8 @@ qbPPrmQPgv5prRHCObn0+j6SwV9vV7Q9BI41CloKUDXZmPFTVipP6z5tV2YTOg== ` func init() { - ioutil.WriteFile("server.crt", []byte(serverCert), 0777) - ioutil.WriteFile("server.key", []byte(serverKey), 0777) - ioutil.WriteFile("client.crt", []byte(clientCert), 0777) - ioutil.WriteFile("client.key", []byte(clientKey), 0777) + ioutil.WriteFile("server.crt", []byte(serverCert), 0o777) + ioutil.WriteFile("server.key", []byte(serverKey), 0o777) + ioutil.WriteFile("client.crt", []byte(clientCert), 0o777) + ioutil.WriteFile("client.key", []byte(clientKey), 0o777) } diff --git a/common/geodata/decode_test.go b/common/geodata/decode_test.go index d4b84d3..0c9e164 100644 --- a/common/geodata/decode_test.go +++ b/common/geodata/decode_test.go @@ -29,13 +29,13 @@ func init() { geositePath := common.GetAssetLocation("geosite.dat") if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) + common.Must(os.MkdirAll(tempPath, 0o755)) geoipBytes, err := common.FetchHTTPContent(geoipURL) common.Must(err) common.Must(common.WriteFile(geoipPath, geoipBytes)) } if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) + common.Must(os.MkdirAll(tempPath, 0o755)) geositeBytes, err := common.FetchHTTPContent(geositeURL) common.Must(err) common.Must(common.WriteFile(geositePath, geositeBytes)) diff --git a/log/log.go b/log/log.go index 929bd57..bbd380a 100644 --- a/log/log.go +++ b/log/log.go @@ -5,8 +5,8 @@ import ( "os" ) -//LogLevel how much log to dump -//0: ALL; 1: INFO; 2: WARN; 3: ERROR; 4: FATAL; 5: OFF +// LogLevel how much log to dump +// 0: ALL; 1: INFO; 2: WARN; 3: ERROR; 4: FATAL; 5: OFF type LogLevel int const ( diff --git a/log/simplelog/simplelog.go b/log/simplelog/simplelog.go index aea5175..467a9b1 100644 --- a/log/simplelog/simplelog.go +++ b/log/simplelog/simplelog.go @@ -95,5 +95,5 @@ func (l *SimpleLogger) Tracef(format string, v ...interface{}) { } func (l *SimpleLogger) SetOutput(io.Writer) { - //do nothing + // do nothing } diff --git a/proxy/proxy.go b/proxy/proxy.go index 631bcda..c5a286a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -190,7 +190,7 @@ func NewProxyFromConfigData(data []byte, isJSON bool) (*Proxy, error) { } log.SetLogLevel(log.LogLevel(cfg.LogLevel)) if cfg.LogFile != "" { - file, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + file, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) if err != nil { return nil, common.NewError("failed to open log file").Base(err) } diff --git a/proxy/server/server.go b/proxy/server/server.go index baa5366..20f4ba3 100644 --- a/proxy/server/server.go +++ b/proxy/server/server.go @@ -67,5 +67,4 @@ func init() { } return proxy.NewProxy(ctx, cancel, serverList, clientList), nil }) - } diff --git a/statistic/mysql/mysql.go b/statistic/mysql/mysql.go index 7fc1af1..9431005 100644 --- a/statistic/mysql/mysql.go +++ b/statistic/mysql/mysql.go @@ -7,12 +7,11 @@ import ( "strings" "time" - "github.com/p4gefau1t/trojan-go/config" - // MySQL Driver _ "github.com/go-sql-driver/mysql" "github.com/p4gefau1t/trojan-go/common" + "github.com/p4gefau1t/trojan-go/config" "github.com/p4gefau1t/trojan-go/log" "github.com/p4gefau1t/trojan-go/statistic" "github.com/p4gefau1t/trojan-go/statistic/memory" @@ -30,7 +29,7 @@ type Authenticator struct { func (a *Authenticator) updater() { for { for _, user := range a.ListUsers() { - //swap upload and download for users + // swap upload and download for users hash := user.Hash() sent, recv := user.ResetTraffic() @@ -47,7 +46,7 @@ func (a *Authenticator) updater() { } log.Info("buffered data has been written into the database") - //update memory + // update memory rows, err := a.db.Query("SELECT password,quota,download,upload FROM users") if err != nil { log.Error(common.NewError("failed to pull data from the database").Base(err)) diff --git a/statistic/statistics.go b/statistic/statistics.go index d796523..411d410 100644 --- a/statistic/statistics.go +++ b/statistic/statistics.go @@ -15,11 +15,11 @@ type TrafficMeter interface { Hash() string AddTraffic(sent, recv int) GetTraffic() (sent, recv uint64) + SetTraffic(sent, recv uint64) ResetTraffic() (sent, recv uint64) GetSpeed() (sent, recv uint64) - SetSpeedLimit(sent, recv int) GetSpeedLimit() (sent, recv int) - SetTraffic(sent, recv uint64) + SetSpeedLimit(sent, recv int) } type IPRecorder interface { @@ -45,9 +45,11 @@ type Authenticator interface { type Creator func(ctx context.Context) (Authenticator, error) -var authCreators = map[string]Creator{} -var createdAuth = map[context.Context]Authenticator{} -var createdAuthLock = sync.Mutex{} +var ( + createdAuthLock sync.Mutex + authCreators = make(map[string]Creator) + createdAuth = make(map[context.Context]Authenticator) +) func RegisterAuthenticatorCreator(name string, creator Creator) { authCreators[name] = creator diff --git a/test/scenario/proxy_test.go b/test/scenario/proxy_test.go index dd4d28f..41eb35f 100644 --- a/test/scenario/proxy_test.go +++ b/test/scenario/proxy_test.go @@ -81,8 +81,8 @@ NYpAJoagHIeNLGo4aJFwiVsZ ` func init() { - ioutil.WriteFile("server.crt", []byte(cert), 0777) - ioutil.WriteFile("server.key", []byte(key), 0777) + ioutil.WriteFile("server.crt", []byte(cert), 0o777) + ioutil.WriteFile("server.key", []byte(key), 0o777) } func CheckClientServer(clientData, serverData string, socksPort int) (ok bool) { @@ -457,7 +457,7 @@ api: time.Sleep(time.Second * 3) client.Close() time.Sleep(time.Second * 3) - //http.ListenAndServe("localhost:6060", nil) + // http.ListenAndServe("localhost:6060", nil) } func SingleThreadBenchmark(clientData, serverData string, socksPort int) { diff --git a/test/util/target.go b/test/util/target.go index a6da498..21d849e 100644 --- a/test/util/target.go +++ b/test/util/target.go @@ -16,8 +16,10 @@ import ( "github.com/p4gefau1t/trojan-go/log" ) -var HTTPAddr string -var HTTPPort string +var ( + HTTPAddr string + HTTPPort string +) func runHelloHTTPServer() { httpHello := func(w http.ResponseWriter, req *http.Request) { @@ -51,8 +53,10 @@ func runHelloHTTPServer() { wg.Done() } -var EchoAddr string -var EchoPort int +var ( + EchoAddr string + EchoPort int +) func runTCPEchoServer() { listener, err := net.Listen("tcp", EchoAddr) @@ -108,8 +112,10 @@ func GeneratePayload(length int) []byte { return buf } -var BlackHoleAddr string -var BlackHolePort int +var ( + BlackHoleAddr string + BlackHolePort int +) func runTCPBlackHoleServer() { listener, err := net.Listen("tcp", BlackHoleAddr) diff --git a/tunnel/freedom/conn.go b/tunnel/freedom/conn.go index 8ef5456..75d8783 100644 --- a/tunnel/freedom/conn.go +++ b/tunnel/freedom/conn.go @@ -65,7 +65,7 @@ type SocksPacketConn struct { func (c *SocksPacketConn) WriteWithMetadata(payload []byte, metadata *tunnel.Metadata) (int, error) { buf := bytes.NewBuffer(make([]byte, 0, MaxPacketSize)) - buf.Write([]byte{0, 0, 0}) //RSV, FRAG + buf.Write([]byte{0, 0, 0}) // RSV, FRAG common.Must(metadata.Address.WriteTo(buf)) buf.Write(payload) _, err := c.PacketConn.WriteTo(buf.Bytes(), c.socksAddr) diff --git a/tunnel/http/server.go b/tunnel/http/server.go index ad349b8..b274270 100644 --- a/tunnel/http/server.go +++ b/tunnel/http/server.go @@ -26,7 +26,7 @@ func (c *ConnectConn) Metadata() *tunnel.Metadata { type OtherConn struct { net.Conn - metadata *tunnel.Metadata //fixed + metadata *tunnel.Metadata // fixed reqReader *io.PipeReader respWriter *io.PipeWriter ctx context.Context @@ -156,7 +156,7 @@ func (s *Server) acceptLoop() { req.Body.Close() resp.Body.Close() - req, err = http.ReadRequest(reqBufReader) //read the next http request from local + req, err = http.ReadRequest(reqBufReader) // read the next http request from local if err != nil { log.Error(common.NewError("http failed to the read request from local").Base(err)) return diff --git a/tunnel/metadata.go b/tunnel/metadata.go index afdbde3..07dbc67 100644 --- a/tunnel/metadata.go +++ b/tunnel/metadata.go @@ -39,7 +39,7 @@ func (r *Metadata) WriteTo(w io.Writer) error { if err := r.Address.WriteTo(buf); err != nil { return err } - //use tcp by default + // use tcp by default r.Address.NetworkType = "tcp" _, err := w.Write(buf.Bytes()) return err @@ -171,7 +171,7 @@ func (a *Address) ReadFrom(r io.Reader) error { if err != nil { return common.NewError("failed to read domain name") } - //the fucking browser uses IP as a domain name sometimes + // the fucking browser uses IP as a domain name sometimes host := buf[0:length] if ip := net.ParseIP(string(host)); ip != nil { a.IP = ip diff --git a/tunnel/mux/client.go b/tunnel/mux/client.go index d863f26..720293c 100644 --- a/tunnel/mux/client.go +++ b/tunnel/mux/client.go @@ -28,7 +28,7 @@ type smuxClientInfo struct { underlayConn tunnel.Conn } -//Client is a smux client +// Client is a smux client type Client struct { clientPoolLock sync.Mutex clientPool map[muxID]*smuxClientInfo @@ -114,7 +114,7 @@ func (c *Client) newMuxClient() (*smuxClientInfo, error) { conn = newStickyConn(conn) smuxConfig := smux.DefaultConfig() - //smuxConfig.KeepAliveDisabled = true + // smuxConfig.KeepAliveDisabled = true client, err := smux.Client(conn, smuxConfig) info := &smuxClientInfo{ client: client, diff --git a/tunnel/mux/config.go b/tunnel/mux/config.go index 35570f3..02fdf46 100644 --- a/tunnel/mux/config.go +++ b/tunnel/mux/config.go @@ -22,5 +22,4 @@ func init() { }, } }) - } diff --git a/tunnel/mux/conn.go b/tunnel/mux/conn.go index cf852d3..5b2d958 100644 --- a/tunnel/mux/conn.go +++ b/tunnel/mux/conn.go @@ -48,7 +48,7 @@ func (c *stickyConn) Close() error { func (c *stickyConn) Write(p []byte) (int, error) { if len(p) == 8 { - if p[0] == 1 || p[0] == 2 { //smux 8 bytes header + if p[0] == 1 || p[0] == 2 { // smux 8 bytes header switch p[1] { // THE CONTENT OF THE BUFFER MIGHT CHANGE // NEVER STORE THE POINTER TO HEADER, COPY THE HEADER INSTEAD diff --git a/tunnel/mux/server.go b/tunnel/mux/server.go index 40d22a0..7724f94 100644 --- a/tunnel/mux/server.go +++ b/tunnel/mux/server.go @@ -32,7 +32,7 @@ func (s *Server) acceptConnWorker() { } go func(conn tunnel.Conn) { smuxConfig := smux.DefaultConfig() - //smuxConfig.KeepAliveDisabled = true + // smuxConfig.KeepAliveDisabled = true smuxSession, err := smux.Server(conn, smuxConfig) if err != nil { log.Error(err) diff --git a/tunnel/router/client.go b/tunnel/router/client.go index 33ed00a..30874a7 100644 --- a/tunnel/router/client.go +++ b/tunnel/router/client.go @@ -52,7 +52,7 @@ func matchDomain(list []*v2router.Domain, target string) bool { } } case v2router.Domain_Plain: - //keyword + // keyword if strings.Contains(target, d.GetValue()) { log.Tracef("domain %s hit keyword rule: %s", target, d.GetValue()) return true @@ -85,11 +85,11 @@ func matchIP(list []*v2router.CIDR, target net.IP) bool { n := int(c.GetPrefix()) mask := net.CIDRMask(n, 8*len) cidrIP := net.IP(c.GetIp()) - if cidrIP.To4() != nil { //IPv4 CIDR + if cidrIP.To4() != nil { // IPv4 CIDR if isIPv6 { continue } - } else { //IPv6 CIDR + } else { // IPv6 CIDR if !isIPv6 { continue } diff --git a/tunnel/router/router_test.go b/tunnel/router/router_test.go index 00e16f6..49e4cac 100644 --- a/tunnel/router/router_test.go +++ b/tunnel/router/router_test.go @@ -28,8 +28,7 @@ func (m MockClient) Close() error { return nil } -type MockPacketConn struct { -} +type MockPacketConn struct{} func (m MockPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { panic("implement me") diff --git a/tunnel/socks/server.go b/tunnel/socks/server.go index b6350ae..ddd2338 100644 --- a/tunnel/socks/server.go +++ b/tunnel/socks/server.go @@ -145,7 +145,7 @@ func (s *Server) packetDispatchLoop() { select { case info := <-conn.output: buf := bytes.NewBuffer(make([]byte, 0, MaxPacketSize)) - buf.Write([]byte{0, 0, 0}) //RSV, FRAG + buf.Write([]byte{0, 0, 0}) // RSV, FRAG common.Must(info.metadata.Address.WriteTo(buf)) buf.Write(info.payload) _, err := s.listenPacketConn.WriteTo(buf.Bytes(), conn.src) diff --git a/tunnel/socks/socks_test.go b/tunnel/socks/socks_test.go index fd5a763..7697cd7 100644 --- a/tunnel/socks/socks_test.go +++ b/tunnel/socks/socks_test.go @@ -73,7 +73,7 @@ func TestSocks(t *testing.T) { payload := util.GeneratePayload(1024) buf := bytes.NewBuffer(make([]byte, 0, 4096)) - buf.Write([]byte{0, 0, 0}) //RSV, FRAG + buf.Write([]byte{0, 0, 0}) // RSV, FRAG common.Must(addr.WriteTo(buf)) buf.Write(payload) diff --git a/tunnel/socks/tunnel.go b/tunnel/socks/tunnel.go index 9486bf2..7ab6b29 100644 --- a/tunnel/socks/tunnel.go +++ b/tunnel/socks/tunnel.go @@ -13,9 +13,11 @@ type Tunnel struct{} func (*Tunnel) Name() string { return Name } + func (*Tunnel) NewClient(context.Context, tunnel.Client) (tunnel.Client, error) { panic("not supported") } + func (*Tunnel) NewServer(ctx context.Context, server tunnel.Server) (tunnel.Server, error) { return NewServer(ctx, server) } diff --git a/tunnel/tls/server.go b/tunnel/tls/server.go index ccfd70b..c3ccd14 100644 --- a/tunnel/tls/server.go +++ b/tunnel/tls/server.go @@ -77,7 +77,6 @@ func (s *Server) acceptLoop() { return } go func(conn net.Conn) { - tlsConfig := &tls.Config{ CipherSuites: s.cipherSuite, PreferServerCipherSuites: s.PreferServerCipher, @@ -315,7 +314,7 @@ func NewServer(ctx context.Context, underlay tunnel.Server) (*Server, error) { var keyLogger io.WriteCloser if cfg.TLS.KeyLogPath != "" { log.Warn("tls key logging activated. USE OF KEY LOGGING COMPROMISES SECURITY. IT SHOULD ONLY BE USED FOR DEBUGGING.") - file, err := os.OpenFile(cfg.TLS.KeyLogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) + file, err := os.OpenFile(cfg.TLS.KeyLogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { return nil, common.NewError("failed to open key log file").Base(err) } diff --git a/tunnel/tls/tls_test.go b/tunnel/tls/tls_test.go index 4a23555..31e683d 100644 --- a/tunnel/tls/tls_test.go +++ b/tunnel/tls/tls_test.go @@ -69,8 +69,8 @@ WS94/5WE/lwHJi8ZPSjH1AURCzXhUi4fGvBrNBtry95e+jcEvP5c0g== ` func TestDefaultTLS(t *testing.T) { - ioutil.WriteFile("server.crt", []byte(cert), 0777) - ioutil.WriteFile("server.key", []byte(key), 0777) + ioutil.WriteFile("server.crt", []byte(cert), 0o777) + ioutil.WriteFile("server.key", []byte(key), 0o777) serverCfg := &Config{ TLS: TLSConfig{ VerifyHostName: true, @@ -131,8 +131,8 @@ func TestDefaultTLS(t *testing.T) { } func TestUTLS(t *testing.T) { - ioutil.WriteFile("server.crt", []byte(cert), 0777) - ioutil.WriteFile("server.key", []byte(key), 0777) + ioutil.WriteFile("server.crt", []byte(cert), 0o777) + ioutil.WriteFile("server.key", []byte(key), 0o777) fingerprints := []string{ "chrome", "firefox", diff --git a/tunnel/trojan/packet.go b/tunnel/trojan/packet.go index 4946057..9bdbb1b 100644 --- a/tunnel/trojan/packet.go +++ b/tunnel/trojan/packet.go @@ -72,7 +72,7 @@ func (c *PacketConn) ReadWithMetadata(payload []byte) (int, *tunnel.Metadata, er } if len(payload) < length || length > MaxPacketSize { - io.CopyN(ioutil.Discard, c.Conn, int64(length)) //drain the rest of the packet + io.CopyN(ioutil.Discard, c.Conn, int64(length)) // drain the rest of the packet return 0, nil, common.NewError("incoming packet size is too large") } _, err = io.ReadFull(c.Conn, payload[:length]) diff --git a/tunnel/trojan/trojan_test.go b/tunnel/trojan/trojan_test.go index 2295337..cda1416 100644 --- a/tunnel/trojan/trojan_test.go +++ b/tunnel/trojan/trojan_test.go @@ -86,7 +86,7 @@ func TestTrojan(t *testing.T) { t.Fail() } - //redirecting + // redirecting conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port)) common.Must(err) sendBuf := util.GeneratePayload(1024) diff --git a/tunnel/websocket/server.go b/tunnel/websocket/server.go index 3d1925e..7ca0a86 100644 --- a/tunnel/websocket/server.go +++ b/tunnel/websocket/server.go @@ -66,7 +66,6 @@ func (s *Server) AcceptConn(tunnel.Tunnel) (tunnel.Conn, error) { defer rewindConn.StopBuffering() rw := bufio.NewReadWriter(bufio.NewReader(rewindConn), bufio.NewWriter(rewindConn)) req, err := http.ReadRequest(rw.Reader) - if err != nil { log.Debug("invalid http request") rewindConn.Rewind() diff --git a/url/share_link_test.go b/url/share_link_test.go index 4b30079..cf0b052 100644 --- a/url/share_link_test.go +++ b/url/share_link_test.go @@ -122,7 +122,6 @@ func TestNewShareInfoFromURL_BadQuery(t *testing.T) { _, e := NewShareInfoFromURL(testCase) assert.Error(t, e, "parse bad query should error") } - } func TestNewShareInfoFromURL_SNI_Empty(t *testing.T) {