github.com/ipfans/trojan-go@v0.11.0/proxy/forward/forward.go (about)

     1  package forward
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ipfans/trojan-go/config"
     7  	"github.com/ipfans/trojan-go/proxy"
     8  	"github.com/ipfans/trojan-go/proxy/client"
     9  	"github.com/ipfans/trojan-go/tunnel"
    10  	"github.com/ipfans/trojan-go/tunnel/dokodemo"
    11  )
    12  
    13  const Name = "FORWARD"
    14  
    15  func init() {
    16  	proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) {
    17  		cfg := config.FromContext(ctx, Name).(*client.Config)
    18  		ctx, cancel := context.WithCancel(ctx)
    19  		serverStack := []string{dokodemo.Name}
    20  		clientStack := client.GenerateClientTree(cfg.TransportPlugin.Enabled, cfg.Mux.Enabled, cfg.Websocket.Enabled, cfg.Shadowsocks.Enabled, cfg.Router.Enabled)
    21  		c, err := proxy.CreateClientStack(ctx, clientStack)
    22  		if err != nil {
    23  			cancel()
    24  			return nil, err
    25  		}
    26  		s, err := proxy.CreateServerStack(ctx, serverStack)
    27  		if err != nil {
    28  			cancel()
    29  			return nil, err
    30  		}
    31  		return proxy.NewProxy(ctx, cancel, []tunnel.Server{s}, c), nil
    32  	})
    33  }
    34  
    35  func init() {
    36  	config.RegisterConfigCreator(Name, func() interface{} {
    37  		return new(client.Config)
    38  	})
    39  }