Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
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 := pxlocal.ParseURL(localAddr, pxlocal.URLOpts{DefaultScheme: 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))
	}

	client := pxlocal.NewClient(cfg.Server.Addr)
	fmt.Println("proxy URL:", pURL)
	failCount := 0
	for {
		px, err := client.RunProxy(pxlocal.ProxyOptions{
			Proto:      pxlocal.ProxyProtocol(cfg.Proto),
			Subdomain:  cfg.SubDomain,
			LocalAddr:  localAddr,
			ListenPort: cfg.ProxyPort,
		})
		if err == nil {
			err = px.Wait()
		}
		if err == pxlocal.ErrWebsocketBroken {
			failCount = 0
			fmt.Println("Reconnect after 5 seconds ...")
			time.Sleep(5 * time.Second)
			continue
		}
		if err == pxlocal.ErrDialTCP {
			if failCount < 13 {
				failCount += 1
			}
			wait := 7 + failCount
			fmt.Printf("Reconnect after %d seconds ...\n", wait)
			time.Sleep(time.Duration(wait) * time.Second)
			continue
		}
		log.Fatal(err)
	}
}