dongyi1939 2016-01-15 13:14
浏览 68

Golang ReverseProxy到不同的ip不起作用

i am trying to make my own ReverseProxy in go. So it should connect my Apache Webserver with another Go Webserver.

So when I request "/" the proxy should send the request to apache and serve the website of the apache server. When I request "/gotest" it should send the request to the go webserver

But the ReverseProxy and the Apache Webserver should run on different ip-adresses.But all should run with SSL. Example: ReverseProxy runs on : 192.168.2.1 Apache Server runs on: 192.168.2.5

If both run on same ip but different ports, it works verry well. But when I run Apache on different ip, the reverseproxy get one request and website is shown. But then my reverseproxy doesn't get new reqeusts.

Have anyone an idea what I am doing wrong?

type Proxy struct {
    listener    net.Listener
    TlsConfig   *tls.Config //config of the TLS-Connection
    mux         *http.ServeMux
    goHandler   http.Handler //Handler to send Requests to GO-Webserver
    mainHandler http.Handler //Handler to send Requests to the main webserver
    Host        string       //Host of the Reverse-Proxy
    Port        int          //Port of the Reverse-Proxy
}


//New returns a new ReverseProxy with the configuration of the given toml file
func New(configPath string) *Proxy {

    if err := common.READCONFIG(configPath); err != nil {
        log.Fatalf(err.Error())
    }

    p := &Proxy{
        mux:  http.NewServeMux(),
        Host: common.CONFIG.ReverseProxy.Host,
        Port: common.CONFIG.ReverseProxy.Port,
    }

    p.goHandler = &httputil.ReverseProxy{
        Director:      p.directorGo,
        FlushInterval: 1 * time.Millisecond,
    }

    p.mainHandler = &httputil.ReverseProxy{
        Director:      p.directorMain,
        FlushInterval: 1 * time.Millisecond,
    }

    pathCert := common.CONFIG.PathCert
    pathKey := common.CONFIG.PathPrivateKey
    //Loads the ssl-cert and private-keyn
    cert, err := tls.LoadX509KeyPair(pathCert, pathKey)
    if err != nil {
        common.Log.Critical("ReverseProxy: load SSL-Keys: %s", err)
    }
    //Change Transport to avoid ssl-certificate error:"certificate signed by unknown authority"
    http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

    p.TlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}

    listener, err := net.Listen("tcp",
        net.JoinHostPort(p.Host, strconv.Itoa(p.Port)))

    if err != nil {
        common.Log.Critical("ReverseProxy: can't listen: %s", err)
        os.Exit(1)
    }

    p.listener = tls.NewListener(listener, p.TlsConfig)

    //Each request should be handled by the proxy
    p.mux.Handle("/", p)

    return p
}

//isGoRequest checks if the given Request is for the go-webserver
func (p *Proxy) getProxyAdress() string {
    return fmt.Sprintf("%s:%d", p.Host, p.Port)
}

//isGoRequest checks if the given Request is for the go-webserver
func (p *Proxy) isGoRequest(req *http.Request) bool {
    if strings.Contains(req.URL.Path, common.CONFIG.ReverseProxy.GoUrlPath) {
        return true
    } else {
        return false
    }
}

//directorMain modifies the request to a new request for the main-webserver
func (p *Proxy) directorMain(req *http.Request) {
    mainServer := fmt.Sprintf("%s:%d", common.CONFIG.ReverseProxy.HostMain, common.CONFIG.ReverseProxy.PortMain)
    req.URL.Scheme = "https"
    req.URL.Host = mainServer
}

//directorGo modifies the request to a new request for the go-webserver
func (p *Proxy) directorGo(req *http.Request) {
    goServer := fmt.Sprintf("%s:%d", common.CONFIG.Pointstreamer.Host, common.CONFIG.Pointstreamer.Port)
    req.URL.Scheme = "https"
    req.URL.Host = goServer
}

// ServeHTTP implements the http.Handler interface.
func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    fmt.Println(req.URL.Path)
    if p.isGoRequest(req) {
        p.goHandler.ServeHTTP(rw, req)
        return
    }
    p.mainHandler.ServeHTTP(rw, req)
}

func main() {
    var configPath = flag.String("conf", "../../configDevel.toml", "Path to the toml config file.")

    flag.Parse()
    proxy := New(*configPath)

    httpsServer := &http.Server{
        Handler:   proxy.mux,
        TLSConfig: proxy.TlsConfig,
    }
    common.Log.Info("Server listens on", proxy.getProxyAdress(), "Main Redirect", common.CONFIG.ReverseProxy.HostMain)

    if err := httpsServer.Serve(proxy.listener); err != nil {
        common.Log.Critical("ReverseProxy Server Error:", err)
        os.Exit(1)
    }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法