mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
Style: format code by gofumpt (#328)
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
+2
-2
@@ -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 (
|
||||
|
||||
@@ -95,5 +95,5 @@ func (l *SimpleLogger) Tracef(format string, v ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *SimpleLogger) SetOutput(io.Writer) {
|
||||
//do nothing
|
||||
// do nothing
|
||||
}
|
||||
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
|
||||
@@ -67,5 +67,4 @@ func init() {
|
||||
}
|
||||
return proxy.NewProxy(ctx, cancel, serverList, clientList), nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+12
-6
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -22,5 +22,4 @@ func init() {
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user