config.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "github.com/snail007/goproxy/services"
  8. "github.com/snail007/goproxy/utils"
  9. kingpin "gopkg.in/alecthomas/kingpin.v2"
  10. )
  11. var (
  12. app *kingpin.Application
  13. service *services.ServiceItem
  14. )
  15. func initConfig() (err error) {
  16. //keygen
  17. if len(os.Args) > 1 {
  18. if os.Args[1] == "keygen" {
  19. utils.Keygen()
  20. os.Exit(0)
  21. }
  22. }
  23. args := services.Args{}
  24. //define args
  25. tcpArgs := services.TCPArgs{}
  26. httpArgs := services.HTTPArgs{}
  27. tunnelServerArgs := services.TunnelServerArgs{}
  28. tunnelClientArgs := services.TunnelClientArgs{}
  29. tunnelBridgeArgs := services.TunnelBridgeArgs{}
  30. udpArgs := services.UDPArgs{}
  31. //build srvice args
  32. app = kingpin.New("proxy", "happy with proxy")
  33. app.Author("snail").Version(APP_VERSION)
  34. args.Parent = app.Flag("parent", "parent address, such as: \"23.32.32.19:28008\"").Default("").Short('P').String()
  35. args.Local = app.Flag("local", "local ip:port to listen").Short('p').Default(":33080").String()
  36. certTLS := app.Flag("cert", "cert file for tls").Short('C').Default("proxy.crt").String()
  37. keyTLS := app.Flag("key", "key file for tls").Short('K').Default("proxy.key").String()
  38. //########http#########
  39. http := app.Command("http", "proxy on http mode")
  40. httpArgs.LocalType = http.Flag("local-type", "parent protocol type <tls|tcp>").Default("tcp").Short('t').Enum("tls", "tcp")
  41. httpArgs.ParentType = http.Flag("parent-type", "parent protocol type <tls|tcp>").Short('T').Enum("tls", "tcp")
  42. httpArgs.Always = http.Flag("always", "always use parent proxy").Default("false").Bool()
  43. httpArgs.Timeout = http.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Default("2000").Int()
  44. httpArgs.HTTPTimeout = http.Flag("http-timeout", "check domain if blocked , http request timeout milliseconds when connect to host").Default("3000").Int()
  45. httpArgs.Interval = http.Flag("interval", "check domain if blocked every interval seconds").Default("10").Int()
  46. httpArgs.Blocked = http.Flag("blocked", "blocked domain file , one domain each line").Default("blocked").Short('b').String()
  47. httpArgs.Direct = http.Flag("direct", "direct domain file , one domain each line").Default("direct").Short('d').String()
  48. httpArgs.AuthFile = http.Flag("auth-file", "http basic auth file,\"username:password\" each line in file").Short('F').String()
  49. httpArgs.Auth = http.Flag("auth", "http basic auth username and password, mutiple user repeat -a ,such as: -a user1:pass1 -a user2:pass2").Short('a').Strings()
  50. httpArgs.PoolSize = http.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
  51. httpArgs.CheckParentInterval = http.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
  52. //########tcp#########
  53. tcp := app.Command("tcp", "proxy on tcp mode")
  54. tcpArgs.Timeout = tcp.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Short('t').Default("2000").Int()
  55. tcpArgs.ParentType = tcp.Flag("parent-type", "parent protocol type <tls|tcp|udp>").Short('T').Enum("tls", "tcp", "udp")
  56. tcpArgs.IsTLS = tcp.Flag("tls", "proxy on tls mode").Default("false").Bool()
  57. tcpArgs.PoolSize = tcp.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
  58. tcpArgs.CheckParentInterval = tcp.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
  59. //########udp#########
  60. udp := app.Command("udp", "proxy on udp mode")
  61. udpArgs.Timeout = udp.Flag("timeout", "tcp timeout milliseconds when connect to parent proxy").Short('t').Default("2000").Int()
  62. udpArgs.ParentType = udp.Flag("parent-type", "parent protocol type <tls|tcp|udp>").Short('T').Enum("tls", "tcp", "udp")
  63. udpArgs.PoolSize = udp.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
  64. udpArgs.CheckParentInterval = udp.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
  65. //########tunnel-server#########
  66. tunnelServer := app.Command("tserver", "proxy on tunnel server mode")
  67. tunnelServerArgs.Timeout = tunnelServer.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
  68. tunnelServerArgs.IsUDP = tunnelServer.Flag("udp", "proxy on udp tunnel server mode").Default("false").Bool()
  69. tunnelServerArgs.Key = tunnelServer.Flag("k", "key same with client").Default("default").String()
  70. //########tunnel-client#########
  71. tunnelClient := app.Command("tclient", "proxy on tunnel client mode")
  72. tunnelClientArgs.Timeout = tunnelClient.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
  73. tunnelClientArgs.IsUDP = tunnelClient.Flag("udp", "proxy on udp tunnel client mode").Default("false").Bool()
  74. tunnelClientArgs.Key = tunnelClient.Flag("k", "key same with server").Default("default").String()
  75. //########tunnel-bridge#########
  76. tunnelBridge := app.Command("tbridge", "proxy on tunnel bridge mode")
  77. tunnelBridgeArgs.Timeout = tunnelBridge.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
  78. kingpin.MustParse(app.Parse(os.Args[1:]))
  79. if *certTLS != "" && *keyTLS != "" {
  80. args.CertBytes, args.KeyBytes = tlsBytes(*certTLS, *keyTLS)
  81. }
  82. //common args
  83. httpArgs.Args = args
  84. tcpArgs.Args = args
  85. udpArgs.Args = args
  86. tunnelBridgeArgs.Args = args
  87. tunnelClientArgs.Args = args
  88. tunnelServerArgs.Args = args
  89. poster()
  90. //regist services and run service
  91. serviceName := kingpin.MustParse(app.Parse(os.Args[1:]))
  92. services.Regist("http", services.NewHTTP(), httpArgs)
  93. services.Regist("tcp", services.NewTCP(), tcpArgs)
  94. services.Regist("udp", services.NewUDP(), udpArgs)
  95. services.Regist("tserver", services.NewTunnelServer(), tunnelServerArgs)
  96. services.Regist("tclient", services.NewTunnelClient(), tunnelClientArgs)
  97. services.Regist("tbridge", services.NewTunnelBridge(), tunnelBridgeArgs)
  98. service, err = services.Run(serviceName)
  99. if err != nil {
  100. log.Fatalf("run service [%s] fail, ERR:%s", service, err)
  101. }
  102. return
  103. }
  104. func poster() {
  105. fmt.Printf(`
  106. ######## ######## ####### ## ## ## ##
  107. ## ## ## ## ## ## ## ## ## ##
  108. ## ## ## ## ## ## ## ## ####
  109. ######## ######## ## ## ### ##
  110. ## ## ## ## ## ## ## ##
  111. ## ## ## ## ## ## ## ##
  112. ## ## ## ####### ## ## ##
  113. v%s`+" by snail , blog : http://www.host900.com/\n\n", APP_VERSION)
  114. }
  115. func tlsBytes(cert, key string) (certBytes, keyBytes []byte) {
  116. certBytes, err := ioutil.ReadFile(cert)
  117. if err != nil {
  118. log.Fatalf("err : %s", err)
  119. return
  120. }
  121. keyBytes, err = ioutil.ReadFile(key)
  122. if err != nil {
  123. log.Fatalf("err : %s", err)
  124. return
  125. }
  126. return
  127. }