fix trojan auth and redir

This commit is contained in:
Page Fault
2020-06-13 03:42:19 +00:00
parent de6b1e33af
commit 9c41490968
7 changed files with 25 additions and 17 deletions
+1
View File
@@ -0,0 +1 @@
package custom
+2 -2
View File
@@ -63,7 +63,7 @@ func (p *Proxy) relayConnLoop() {
defer inbound.Close()
outbound, err := p.sink.DialConn(inbound.Metadata().Address, nil)
if err != nil {
log.Error(err)
log.Error(common.NewError("proxy failed to dial connection").Base(err))
return
}
defer outbound.Close()
@@ -110,7 +110,7 @@ func (p *Proxy) relayPacketLoop() {
defer inbound.Close()
outbound, err := p.sink.DialPacket(nil)
if err != nil {
log.Error(err)
log.Error(common.NewError("proxy failed to dial packet").Base(err))
return
}
defer outbound.Close()
+1 -1
View File
@@ -172,7 +172,7 @@ func NewServer(ctx context.Context, _ tunnel.Server) (*Server, error) {
packetChan: make(chan tunnel.PacketConn, 32),
}
go server.packetDispatchLoop()
log.Info("tproxy server listening on", tcpListener.Addr(), "(udp/tcp)")
log.Info("tproxy server listening on", tcpListener.Addr(), "(tcp)", udpListener.LocalAddr(), "(udp)")
log.Debug("tproxy server created")
return server, nil
}
+14 -12
View File
@@ -29,7 +29,6 @@ type OutboundConn struct {
metadata *tunnel.Metadata
sent uint64
recv uint64
auth statistic.Authenticator
user statistic.User
headerWritten bool
net.Conn
@@ -41,13 +40,7 @@ func (c *OutboundConn) Metadata() *tunnel.Metadata {
func (c *OutboundConn) WriteHeader(payload []byte) error {
if !c.headerWritten {
users := c.auth.ListUsers()
if len(users) == 0 {
return common.NewError("no password found")
}
user := users[0]
hash := user.Hash()
c.user = user
hash := c.user.Hash()
buf := bytes.NewBuffer(make([]byte, 0, MaxPacketSize))
crlf := []byte{0x0d, 0x0a}
buf.Write([]byte(hash))
@@ -93,7 +86,7 @@ func (c *OutboundConn) Close() error {
type Client struct {
underlay tunnel.Client
ctx context.Context
auth statistic.Authenticator
user statistic.User
}
func (c *Client) Close() error {
@@ -107,7 +100,7 @@ func (c *Client) DialConn(addr *tunnel.Address, overlay tunnel.Tunnel) (tunnel.C
}
newConn := &OutboundConn{
Conn: conn,
auth: c.auth,
user: c.user,
metadata: &tunnel.Metadata{
Command: Connect,
Address: addr,
@@ -138,7 +131,7 @@ func (c *Client) DialPacket(tunnel.Tunnel) (tunnel.PacketConn, error) {
return &PacketConn{
Conn: &OutboundConn{
Conn: conn,
auth: c.auth,
user: c.user,
metadata: &tunnel.Metadata{
Command: Associate,
Address: fakeAddr,
@@ -155,10 +148,19 @@ func NewClient(ctx context.Context, client tunnel.Client) (*Client, error) {
go api.RunService(ctx, Name+"_CLIENT", auth)
var user statistic.User
for _, u := range auth.ListUsers() {
user = u
break
}
if user == nil {
return nil, common.NewError("no valid user found")
}
log.Debug("trojan client created")
return &Client{
underlay: client,
ctx: ctx,
auth: auth,
user: user,
}, nil
}
+2 -1
View File
@@ -94,7 +94,7 @@ func (c *InboundConn) Auth() error {
// Server is a trojan tunnel server
type Server struct {
auth statistic.Authenticator
redir redirector.Redirector
redir *redirector.Redirector
redirAddr *tunnel.Address
underlay tunnel.Server
connChan chan tunnel.Conn
@@ -214,6 +214,7 @@ func NewServer(ctx context.Context, underlay tunnel.Server) (tunnel.Server, erro
packetChan: make(chan tunnel.PacketConn, 32),
ctx: ctx,
cancel: cancel,
redir: redirector.NewRedirector(ctx),
}
if !cfg.DisableHTTPCheck {
+1 -1
View File
@@ -65,7 +65,7 @@ func TestTrojan(t *testing.T) {
c := &Client{
underlay: tcpClient,
ctx: ctx,
auth: &MockAuth{},
user: &MockUser{},
}
conn1, err := c.DialConn(&tunnel.Address{
+4
View File
@@ -143,6 +143,10 @@ func NewServer(ctx context.Context, underlay tunnel.Server) (*Server, error) {
return nil, common.NewError("websocket path must start with \"/\"")
}
}
if cfg.Websocket.Hostname == "" {
cfg.Websocket.Hostname = cfg.RemoteHost
log.Warn("empty websocket hostname")
}
ctx, cancel := context.WithCancel(ctx)
log.Debug("websocket server created")
return &Server{