Skip to content

Commit

Permalink
Style: format code by gofumpt (p4gefau1t#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier authored May 19, 2021
1 parent 531bc2b commit 1d6dcf6
Show file tree
Hide file tree
Showing 29 changed files with 60 additions and 57 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions api/service/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions common/geodata/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion log/simplelog/simplelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ func (l *SimpleLogger) Tracef(format string, v ...interface{}) {
}

func (l *SimpleLogger) SetOutput(io.Writer) {
//do nothing
// do nothing
}
2 changes: 1 addition & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion proxy/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ func init() {
}
return proxy.NewProxy(ctx, cancel, serverList, clientList), nil
})

}
7 changes: 3 additions & 4 deletions statistic/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Expand All @@ -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))
Expand Down
12 changes: 7 additions & 5 deletions statistic/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/scenario/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 12 additions & 6 deletions test/util/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tunnel/freedom/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tunnel/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tunnel/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tunnel/mux/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tunnel/mux/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ func init() {
},
}
})

}
2 changes: 1 addition & 1 deletion tunnel/mux/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tunnel/mux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tunnel/router/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions tunnel/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tunnel/socks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tunnel/socks/socks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions tunnel/socks/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions tunnel/tls/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (s *Server) acceptLoop() {
return
}
go func(conn net.Conn) {

tlsConfig := &tls.Config{
CipherSuites: s.cipherSuite,
PreferServerCipherSuites: s.PreferServerCipher,
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions tunnel/tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tunnel/trojan/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion tunnel/trojan/trojan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 1d6dcf6

Please sign in to comment.