forked from codeskyblue/proxylocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (100 loc) · 3.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"github.com/codeskyblue/proxylocal/pxlocal"
"github.com/qiniu/log"
"gopkg.in/alecthomas/kingpin.v2"
)
type GlobalConfig struct {
Server struct {
Enable bool
Addr string
Domain string
}
Proto string
Data string
ProxyPort int
SubDomain string
Debug bool
}
var cfg GlobalConfig
var localAddr string
func init() {
var defaultServerAddr = os.Getenv("PXL_SERVER_ADDR")
if defaultServerAddr == "" {
defaultServerAddr = "proxylocal.xyz:80"
}
kingpin.Flag("debug", "Enable debug mode.").BoolVar(&cfg.Debug)
kingpin.Flag("proto", "Default protocol, http or tcp").Default("http").EnumVar(&cfg.Proto, "http", "tcp") // .StringVar(&cfg.Proto)
kingpin.Flag("subdomain", "Proxy subdomain, used for http").StringVar(&cfg.SubDomain)
kingpin.Flag("remote-port", "Proxy server listen port, only used in tcp").IntVar(&cfg.ProxyPort)
kingpin.Flag("data", "Data send to server, can be anything").StringVar(&cfg.Data)
kingpin.Flag("server", "Specify server address").OverrideDefaultFromEnvar("PXL_SERVER_ADDR").Default(defaultServerAddr).StringVar(&cfg.Server.Addr)
kingpin.Flag("listen", "Run in server mode").Short('l').BoolVar(&cfg.Server.Enable)
kingpin.Flag("domain", "Proxy server mode domain name, optional").StringVar(&cfg.Server.Domain)
kingpin.Arg("local", "Local address").Required().StringVar(&localAddr)
}
func parseURL(addr string, defaultProto string) (u *url.URL, err error) {
if !regexp.MustCompile("^(http|https|tcp)://").MatchString(addr) {
if _, err := strconv.Atoi(addr); err == nil { // only contain port
addr = "localhost:" + addr
} else {
_, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
}
addr = defaultProto + "://" + addr
}
return url.Parse(addr)
}
func main() {
kingpin.Version(VERSION)
kingpin.CommandLine.VersionFlag.Short('v')
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
if !cfg.Server.Enable && localAddr == "" {
kingpin.Usage()
return
}
if !cfg.Debug {
log.SetOutputLevel(log.Linfo)
} else {
log.SetOutputLevel(log.Ldebug)
}
pURL, err := parseURL(localAddr, cfg.Proto)
if err != nil {
log.Fatal(err)
}
if cfg.Server.Enable {
_, port, _ := net.SplitHostPort(pURL.Host)
if port == "" {
port = "80"
}
addr := net.JoinHostPort("0.0.0.0", port)
if cfg.Server.Domain == "" {
cfg.Server.Domain = "localhost" //cfg.Server.Addr
}
fmt.Printf("proxylocal: server listen on %v, domain is %v\n", addr, cfg.Server.Domain)
ps := pxlocal.NewProxyServer(cfg.Server.Domain)
log.Fatal(http.ListenAndServe(addr, ps))
}
// var localAddr = flag.Arg(0)
// if !regexp.MustCompile("^(http|https|tcp)://").MatchString(localAddr) {
// if _, err := strconv.Atoi(localAddr); err == nil { // only contain port
// localAddr = "localhost:" + localAddr
// } else {
// //localAddr += ":80"
// }
// localAddr = cfg.Proto + "://" + localAddr
// }
// pURL, err := url.Parse(localAddr)
fmt.Println("proxy URL:", pURL)
pxlocal.StartAgent(pURL, cfg.SubDomain, cfg.Server.Addr, cfg.ProxyPort, cfg.Data)
}