Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: api add user not work when use websocket #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/scenario/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
_ "github.com/p4gefau1t/trojan-go/proxy/server"
_ "github.com/p4gefau1t/trojan-go/statistic/memory"
"github.com/p4gefau1t/trojan-go/test/util"
"github.com/p4gefau1t/trojan-go/tunnel/trojan"
)

// test key and cert
Expand Down Expand Up @@ -86,6 +87,7 @@ func init() {
}

func CheckClientServer(clientData, serverData string, socksPort int) (ok bool) {
trojan.Auth = nil
server, err := proxy.NewProxyFromConfigData([]byte(serverData), false)
common.Must(err)
go server.Run()
Expand Down
32 changes: 17 additions & 15 deletions tunnel/trojan/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/p4gefau1t/trojan-go/tunnel/mux"
)

var Auth statistic.Authenticator

// InboundConn is a trojan inbound connection
type InboundConn struct {
net.Conn
Expand Down Expand Up @@ -206,29 +208,29 @@ func NewServer(ctx context.Context, underlay tunnel.Server) (*Server, error) {
cfg := config.FromContext(ctx, Name).(*Config)
ctx, cancel := context.WithCancel(ctx)

// TODO replace this dirty code
var auth statistic.Authenticator
var err error
if cfg.MySQL.Enabled {
log.Debug("mysql enabled")
auth, err = statistic.NewAuthenticator(ctx, mysql.Name)
} else {
log.Debug("auth by config file")
auth, err = statistic.NewAuthenticator(ctx, memory.Name)
}
if err != nil {
cancel()
return nil, common.NewError("trojan failed to create authenticator")
if Auth == nil {
var err error
if cfg.MySQL.Enabled {
log.Debug("mysql enabled")
Auth, err = statistic.NewAuthenticator(ctx, mysql.Name)
} else {
log.Debug("auth by config file")
Auth, err = statistic.NewAuthenticator(ctx, memory.Name)
}
if err != nil {
cancel()
return nil, common.NewError("trojan failed to create authenticator")
}
}

if cfg.API.Enabled {
go api.RunService(ctx, Name+"_SERVER", auth)
go api.RunService(ctx, Name+"_SERVER", Auth)
}

redirAddr := tunnel.NewAddressFromHostPort("tcp", cfg.RemoteHost, cfg.RemotePort)
s := &Server{
underlay: underlay,
auth: auth,
auth: Auth,
redirAddr: redirAddr,
connChan: make(chan tunnel.Conn, 32),
muxChan: make(chan tunnel.Conn, 32),
Expand Down