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

     1  package easy
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"net"
     7  	"strconv"
     8  
     9  	"github.com/ipfans/trojan-go/common"
    10  	"github.com/ipfans/trojan-go/log"
    11  	"github.com/ipfans/trojan-go/option"
    12  	"github.com/ipfans/trojan-go/proxy"
    13  )
    14  
    15  type easy struct {
    16  	server   *bool
    17  	client   *bool
    18  	password *string
    19  	local    *string
    20  	remote   *string
    21  	cert     *string
    22  	key      *string
    23  }
    24  
    25  type ClientConfig struct {
    26  	RunType    string   `json:"run_type"`
    27  	LocalAddr  string   `json:"local_addr"`
    28  	LocalPort  int      `json:"local_port"`
    29  	RemoteAddr string   `json:"remote_addr"`
    30  	RemotePort int      `json:"remote_port"`
    31  	Password   []string `json:"password"`
    32  }
    33  
    34  type TLS struct {
    35  	SNI  string `json:"sni"`
    36  	Cert string `json:"cert"`
    37  	Key  string `json:"key"`
    38  }
    39  
    40  type ServerConfig struct {
    41  	RunType    string   `json:"run_type"`
    42  	LocalAddr  string   `json:"local_addr"`
    43  	LocalPort  int      `json:"local_port"`
    44  	RemoteAddr string   `json:"remote_addr"`
    45  	RemotePort int      `json:"remote_port"`
    46  	Password   []string `json:"password"`
    47  	TLS        `json:"ssl"`
    48  }
    49  
    50  func (o *easy) Name() string {
    51  	return "easy"
    52  }
    53  
    54  func (o *easy) Handle() error {
    55  	if !*o.server && !*o.client {
    56  		return common.NewError("empty")
    57  	}
    58  	if *o.password == "" {
    59  		log.Fatal("empty password is not allowed")
    60  	}
    61  	log.Info("easy mode enabled, trojan-go will NOT use the config file")
    62  	if *o.client {
    63  		if *o.local == "" {
    64  			log.Warn("client local addr is unspecified, using 127.0.0.1:1080")
    65  			*o.local = "127.0.0.1:1080"
    66  		}
    67  		localHost, localPortStr, err := net.SplitHostPort(*o.local)
    68  		if err != nil {
    69  			log.Fatal(common.NewError("invalid local addr format:" + *o.local).Base(err))
    70  		}
    71  		remoteHost, remotePortStr, err := net.SplitHostPort(*o.remote)
    72  		if err != nil {
    73  			log.Fatal(common.NewError("invalid remote addr format:" + *o.remote).Base(err))
    74  		}
    75  		localPort, err := strconv.Atoi(localPortStr)
    76  		if err != nil {
    77  			log.Fatal(err)
    78  		}
    79  		remotePort, err := strconv.Atoi(remotePortStr)
    80  		if err != nil {
    81  			log.Fatal(err)
    82  		}
    83  		clientConfig := ClientConfig{
    84  			RunType:    "client",
    85  			LocalAddr:  localHost,
    86  			LocalPort:  localPort,
    87  			RemoteAddr: remoteHost,
    88  			RemotePort: remotePort,
    89  			Password: []string{
    90  				*o.password,
    91  			},
    92  		}
    93  		clientConfigJSON, err := json.Marshal(&clientConfig)
    94  		common.Must(err)
    95  		log.Info("generated config:")
    96  		log.Info(string(clientConfigJSON))
    97  		proxy, err := proxy.NewProxyFromConfigData(clientConfigJSON, true)
    98  		if err != nil {
    99  			log.Fatal(err)
   100  		}
   101  		if err := proxy.Run(); err != nil {
   102  			log.Fatal(err)
   103  		}
   104  	} else if *o.server {
   105  		if *o.remote == "" {
   106  			log.Warn("server remote addr is unspecified, using 127.0.0.1:80")
   107  			*o.remote = "127.0.0.1:80"
   108  		}
   109  		if *o.local == "" {
   110  			log.Warn("server local addr is unspecified, using 0.0.0.0:443")
   111  			*o.local = "0.0.0.0:443"
   112  		}
   113  		localHost, localPortStr, err := net.SplitHostPort(*o.local)
   114  		if err != nil {
   115  			log.Fatal(common.NewError("invalid local addr format:" + *o.local).Base(err))
   116  		}
   117  		remoteHost, remotePortStr, err := net.SplitHostPort(*o.remote)
   118  		if err != nil {
   119  			log.Fatal(common.NewError("invalid remote addr format:" + *o.remote).Base(err))
   120  		}
   121  		localPort, err := strconv.Atoi(localPortStr)
   122  		if err != nil {
   123  			log.Fatal(err)
   124  		}
   125  		remotePort, err := strconv.Atoi(remotePortStr)
   126  		if err != nil {
   127  			log.Fatal(err)
   128  		}
   129  		serverConfig := ServerConfig{
   130  			RunType:    "server",
   131  			LocalAddr:  localHost,
   132  			LocalPort:  localPort,
   133  			RemoteAddr: remoteHost,
   134  			RemotePort: remotePort,
   135  			Password: []string{
   136  				*o.password,
   137  			},
   138  			TLS: TLS{
   139  				Cert: *o.cert,
   140  				Key:  *o.key,
   141  			},
   142  		}
   143  		serverConfigJSON, err := json.Marshal(&serverConfig)
   144  		common.Must(err)
   145  		log.Info("generated json config:")
   146  		log.Info(string(serverConfigJSON))
   147  		proxy, err := proxy.NewProxyFromConfigData(serverConfigJSON, true)
   148  		if err != nil {
   149  			log.Fatal(err)
   150  		}
   151  		if err := proxy.Run(); err != nil {
   152  			log.Fatal(err)
   153  		}
   154  	}
   155  	return nil
   156  }
   157  
   158  func (o *easy) Priority() int {
   159  	return 50
   160  }
   161  
   162  func init() {
   163  	option.RegisterHandler(&easy{
   164  		server:   flag.Bool("server", false, "Run a trojan-go server"),
   165  		client:   flag.Bool("client", false, "Run a trojan-go client"),
   166  		password: flag.String("password", "", "Password for authentication"),
   167  		remote:   flag.String("remote", "", "Remote address, e.g. 127.0.0.1:12345"),
   168  		local:    flag.String("local", "", "Local address, e.g. 127.0.0.1:12345"),
   169  		key:      flag.String("key", "server.key", "Key of the server"),
   170  		cert:     flag.String("cert", "server.crt", "Certificates of the server"),
   171  	})
   172  }