Skip to content

Commit

Permalink
fix trival bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Mar 21, 2020
1 parent d0d0c81 commit 65e0951
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ The format of the configuration file is compatible, see [here](https://trojan-gf
- [x] Mux
- [x] TLS Settings
- [x] TLS redirecting
- [ ] non-TLS redirecting
- [X] non-TLS redirecting
- [ ] Cert utils
- [x] Database support
- [x] Traffic stats
Expand Down
6 changes: 3 additions & 3 deletions protocol/trojan/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func NewOutboundConnSession(req *protocol.Request, conn io.ReadWriteCloser, conf
ClientSessionCache: tls.NewLRUClientSessionCache(-1),
}
tlsConn, err := tls.Dial("tcp", config.RemoteAddr.String(), tlsConfig)
if err != nil {
return nil, common.NewError("cannot dial to the remote server").Base(err)
}
if config.TLS.VerifyHostname {
if err := tlsConn.VerifyHostname(config.TLS.SNI); err != nil {
return nil, common.NewError("failed to verify hostname").Base(err)
}
}
if err != nil {
return nil, common.NewError("cannot dial to the remote server").Base(err)
}
conn = tlsConn
}
o := &TrojanOutboundConnSession{
Expand Down
40 changes: 40 additions & 0 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package proxy

import (
"crypto/x509"
"io/ioutil"
"net"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -40,6 +42,44 @@ func TestClientToPortReusingServer(t *testing.T) {
time.Sleep(time.Hour)
}

func TestSNIConfig(t *testing.T) {
go ClientWithWrongSNI(t)
TestServer(t)
}

func ClientWithWrongSNI(t *testing.T) {
serverCertBytes, err := ioutil.ReadFile("./server.crt")
common.Must(err)
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(serverCertBytes)
ip := net.IPv4(127, 0, 0, 1)
port := 4444
password := "pass123123"
config := &conf.GlobalConfig{
LocalAddr: &net.TCPAddr{
IP: ip,
Port: port,
},
LocalIP: ip,
LocalPort: uint16(port),
RemoteAddr: &net.TCPAddr{
IP: ip,
Port: 4445,
},
Hash: map[string]string{common.SHA224String(password): password},
}
config.TLS.Verify = true
config.TLS.CertPool = pool
config.TLS.SNI = "localhost123"
config.TLS.VerifyHostname = true

c := Client{
config: config,
}
c.Run()
time.Sleep(time.Hour)
}

func BenchmarkClientToServerHugePayload(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("client.json")
Expand Down
4 changes: 3 additions & 1 deletion proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ func (s *Server) Run() error {
err = tlsConn.Handshake()
if err != nil {
logger.Warn(common.NewError("failed to handshake, response http payload").Base(err))
conn.Write(s.config.TLS.HTTPResponse)
if len(s.config.TLS.HTTPResponse) > 0 {
conn.Write(s.config.TLS.HTTPResponse)
}
conn.Close()
continue
}
Expand Down
32 changes: 32 additions & 0 deletions proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,35 @@ func TestPortReusingServer(t *testing.T) {
//common.Must(server2.Run())
//time.Sleep(time.Hour)
}

func TestServerTCPRedirecting(t *testing.T) {
key, err := tls.LoadX509KeyPair("server.crt", "server.key")
common.Must(err)
ip := net.IPv4(127, 0, 0, 1)
port := 4445
password := "pass123123"
config := &conf.GlobalConfig{
LocalAddr: &net.TCPAddr{
IP: ip,
Port: port,
},
LocalIP: ip,
LocalPort: uint16(port),
RemoteAddr: &net.TCPAddr{
IP: ip,
Port: 80,
},
RemoteIP: ip,
RemotePort: 80,
Hash: map[string]string{common.SHA224String(password): password},
}
config.TLS.KeyPair = []tls.Certificate{key}
config.TLS.SNI = "localhost"
payload, err := ioutil.ReadFile("http.txt")
common.Must(err)
config.TLS.HTTPResponse = payload
server := Server{
config: config,
}
server.Run()
}
1 change: 1 addition & 0 deletions stat/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (a *DBAuthenticator) updateDaemon() {
rows, err := a.db.Query("SELECT username,password,quota,download,upload FROM users")
if err != nil {
logger.Error(common.NewError("failed to pull data from the database").Base(err))
time.Sleep(statsUpdateDuration)
continue
}
newValidUsers := make(map[string]string)
Expand Down

0 comments on commit 65e0951

Please sign in to comment.