Skip to content

Commit

Permalink
Clean unnecessary code in Trojan
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX committed Jul 6, 2023
1 parent 846d3eb commit 6d41944
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 97 deletions.
59 changes: 24 additions & 35 deletions infra/conf/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ type TrojanClientConfig struct {

// Build implements Buildable
func (c *TrojanClientConfig) Build() (proto.Message, error) {
config := new(trojan.ClientConfig)

if len(c.Servers) == 0 {
return nil, newError("0 Trojan server configured.")
}

serverSpecs := make([]*protocol.ServerEndpoint, len(c.Servers))
config := &trojan.ClientConfig{
Server: make([]*protocol.ServerEndpoint, len(c.Servers)),
}

for idx, rec := range c.Servers {
if rec.Address == nil {
return nil, newError("Trojan server address is not set.")
Expand All @@ -47,34 +48,25 @@ func (c *TrojanClientConfig) Build() (proto.Message, error) {
if rec.Password == "" {
return nil, newError("Trojan password is not specified.")
}
account := &trojan.Account{
Password: rec.Password,
Flow: rec.Flow,
}

switch account.Flow {
case "":
default:
return nil, newError(`Trojan servers: "flow" doesn't support "` + account.Flow + `" in this version`)
if rec.Flow != "" {
return nil, newError(`Trojan doesn't support "flow" anymore.`)
}

trojan := &protocol.ServerEndpoint{
config.Server[idx] = &protocol.ServerEndpoint{
Address: rec.Address.Build(),
Port: uint32(rec.Port),
User: []*protocol.User{
{
Level: uint32(rec.Level),
Email: rec.Email,
Account: serial.ToTypedMessage(account),
Level: uint32(rec.Level),
Email: rec.Email,
Account: serial.ToTypedMessage(&trojan.Account{
Password: rec.Password,
}),
},
},
}

serverSpecs[idx] = trojan
}

config.Server = serverSpecs

return config, nil
}

Expand Down Expand Up @@ -105,25 +97,22 @@ type TrojanServerConfig struct {

// Build implements Buildable
func (c *TrojanServerConfig) Build() (proto.Message, error) {
config := new(trojan.ServerConfig)
config.Users = make([]*protocol.User, len(c.Clients))
config := &trojan.ServerConfig{
Users: make([]*protocol.User, len(c.Clients)),
}

for idx, rawUser := range c.Clients {
user := new(protocol.User)
account := &trojan.Account{
Password: rawUser.Password,
Flow: rawUser.Flow,
if rawUser.Flow != "" {
return nil, newError(`Trojan doesn't support "flow" anymore.`)
}

switch account.Flow {
case "":
default:
return nil, newError(`Trojan clients: "flow" doesn't support "` + account.Flow + `" in this version`)
config.Users[idx] = &protocol.User{
Level: uint32(rawUser.Level),
Email: rawUser.Email,
Account: serial.ToTypedMessage(&trojan.Account{
Password: rawUser.Password,
}),
}

user.Email = rawUser.Email
user.Level = uint32(rawUser.Level)
user.Account = serial.ToTypedMessage(account)
config.Users[idx] = user
}

if c.Fallback != nil {
Expand Down
18 changes: 5 additions & 13 deletions proxy/trojan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,12 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter

defer conn.Close()

iConn := conn
statConn, ok := iConn.(*stat.CounterConnection)
if ok {
iConn = statConn.Connection
}

user := server.PickUser()
account, ok := user.Account.(*MemoryAccount)
if !ok {
return newError("user account is not valid")
}

connWriter := &ConnWriter{
Flow: account.Flow,
}

var newCtx context.Context
var newCancel context.CancelFunc
if session.TimeoutOnlyFromContext(ctx) {
Expand All @@ -113,9 +103,11 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter

bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn))

connWriter.Writer = bufferWriter
connWriter.Target = destination
connWriter.Account = account
connWriter := &ConnWriter{
Writer: bufferWriter,
Target: destination,
Account: account,
}

var bodyWriter buf.Writer
if destination.Network == net.Network_UDP {
Expand Down
2 changes: 0 additions & 2 deletions proxy/trojan/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type MemoryAccount struct {
Password string
Key []byte
Flow string
}

// AsAccount implements protocol.AsAccount.
Expand All @@ -23,7 +22,6 @@ func (a *Account) AsAccount() (protocol.Account, error) {
return &MemoryAccount{
Password: password,
Key: key,
Flow: a.Flow,
}, nil
}

Expand Down
69 changes: 30 additions & 39 deletions proxy/trojan/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions proxy/trojan/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "common/protocol/server_spec.proto";

message Account {
string password = 1;
string flow = 2;
}

message Fallback {
Expand All @@ -29,5 +28,5 @@ message ClientConfig {

message ServerConfig {
repeated xray.common.protocol.User users = 1;
repeated Fallback fallbacks = 3;
repeated Fallback fallbacks = 2;
}
1 change: 0 additions & 1 deletion proxy/trojan/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type ConnWriter struct {
io.Writer
Target net.Destination
Account *MemoryAccount
Flow string
headerSent bool
}

Expand Down
4 changes: 2 additions & 2 deletions proxy/trojan/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
})

newError("received request for ", destination).WriteToLog(sid)
return s.handleConnection(ctx, sessionPolicy, destination, clientReader, buf.NewWriter(conn), dispatcher, iConn, statConn)
return s.handleConnection(ctx, sessionPolicy, destination, clientReader, buf.NewWriter(conn), dispatcher)
}

func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error {
Expand Down Expand Up @@ -300,7 +300,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Session,
destination net.Destination,
clientReader buf.Reader,
clientWriter buf.Writer, dispatcher routing.Dispatcher, iConn stat.Connection, statConn *stat.CounterConnection,
clientWriter buf.Writer, dispatcher routing.Dispatcher,
) error {
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
Expand Down
4 changes: 1 addition & 3 deletions proxy/trojan/trojan.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package trojan

const (
muxCoolAddress = "v1.mux.cool"
)
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen

0 comments on commit 6d41944

Please sign in to comment.