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) }
func main() { var serverMode bool var serverAddr string var proxyPort int var proxyAddr string var subDomain string var domain string var debug bool var defaultServerAddr = os.Getenv("PXL_SERVER_ADDR") if defaultServerAddr == "" { defaultServerAddr = "proxylocal.xyz" } flag.BoolVar(&serverMode, "server", false, "run in server mode") flag.StringVar(&serverAddr, "server-addr", defaultServerAddr, "server address") flag.StringVar(&domain, "server-domain", "", "proxy server domain name, optional") flag.StringVar(&subDomain, "subdomain", "", "proxy subdomain, used for http") flag.BoolVar(&debug, "debug", false, "open debug mode") flag.IntVar(&proxyPort, "port", 0, "proxy server listen port, used for tcp") flag.Usage = func() { fmt.Printf("Usage: %s [OPTIONS] <port | host:port>\n", os.Args[0]) flag.PrintDefaults() } flag.Parse() if !serverMode && len(flag.Args()) != 1 { flag.Usage() return } if !debug { log.SetOutputLevel(log.Linfo) } else { log.SetOutputLevel(log.Ldebug) } if serverMode { _, port, _ := net.SplitHostPort(serverAddr) if port == "" { port = "80" } addr := net.JoinHostPort("0.0.0.0", port) if domain == "" { domain = serverAddr } fmt.Println("proxylocal: server listen on", addr) ps := pxlocal.NewProxyServer(domain) log.Fatal(http.ListenAndServe(addr, ps)) } proxyAddr = flag.Arg(0) if !regexp.MustCompile("^(http|https|tcp)://").MatchString(proxyAddr) { if _, err := strconv.Atoi(proxyAddr); err == nil { // only contain port proxyAddr = "localhost:" + proxyAddr } else { //proxyAddr += ":80" } proxyAddr = "http://" + proxyAddr } pURL, err := url.Parse(proxyAddr) if err != nil { log.Fatal(err) } log.Println("proxy URL:", pURL) pxlocal.StartAgent(pURL, subDomain, serverAddr, proxyPort) }