Пример #1
0
func main() {

	flag.IntVar(&port, "port", 8080, "the port listen to")
	flag.StringVar(&serverName, "server_name", "", "the server name")
	flag.Var(&hosts, "server", "the server connect to")
	flag.StringVar(&docroot, "docroot", ".", "the local http www root")

	iniflags.Parse()

	initRouters()

	if len(hosts) == 0 {
		log.Fatal("you must special a server")
	}
	http2.VerboseLogs = false
	log.Printf("Listening on :%d", port)
	p := &proxy{}
	p.handler = &http2.Transport{
		DialTLS:   p.dialTLS,
		AllowHTTP: true,
	}
	hdr := &myhandler{proxy: p}
	err := http.ListenAndServe(fmt.Sprintf(":%d", port),
		handlers.CombinedLoggingHandler(os.Stdout, hdr))
	if err != nil {
		log.Fatal(err)
	}
}
Пример #2
0
func initListeners() {
	var out = os.Stdout

	if cfg.LogFile != "" {
		out1, err := os.Create(cfg.LogFile)
		if err != nil {
			log.Print(err)
		} else {
			out = out1
		}
	}
	log.SetOutput(out)
	logger := log.New(out, "", log.LstdFlags)

	for _, l := range cfg.Host {
		hdr1 := &gpp.Handler{
			//Handler:           Router,
			EnableProxy:       l.EnableProxy,
			EnableProxyHTTP11: true,
			LocalDomains:      cfg.LocalDomains,
			Logger:            logger,
			ProxyAuth:         l.ProxyAuth,
			ProxyAuthFunc:     proxyAuthFunc,
		}

		hdr := handlers.CombinedLoggingHandler(out, hdr1)
		if l.Cert != "" && l.Key != "" {
			go func(l ListenEntry) {
				fmt.Printf("Listen on https %s\n", l.Host)
				err := http.ListenAndServeTLS(l.Host, l.Cert, l.Key, hdr)
				if err != nil {
					fmt.Printf("listen failed on %s: %s\n", l.Host, err)
					os.Exit(-1)
				}
			}(l)
		} else {
			go func(l ListenEntry) {
				fmt.Printf("Listen on http %s\n", l.Host)
				err := http.ListenAndServe(l.Host, hdr)
				if err != nil {
					fmt.Printf("listen failed on %s: %s\n", l.Host, err)
					os.Exit(-1)
				}
			}(l)
		}

	}
}