twins-upstream/serve_proxy.go

34 lines
667 B
Go
Raw Normal View History

2020-11-05 05:18:59 +01:00
package main
import (
"crypto/tls"
"io"
"net"
"net/url"
"strings"
)
2020-11-12 18:56:59 +01:00
func serveProxy(c net.Conn, request *url.URL, proxyURL string) int {
2020-11-05 05:18:59 +01:00
tlsConfig := &tls.Config{}
if strings.HasPrefix(proxyURL, "gemini://") {
proxyURL = proxyURL[9:]
} else if strings.HasPrefix(proxyURL, "gemini-insecure://") {
proxyURL = proxyURL[18:]
tlsConfig.InsecureSkipVerify = true
}
proxy, err := tls.Dial("tcp", proxyURL, tlsConfig)
if err != nil {
2020-11-12 18:56:59 +01:00
return writeStatus(c, statusProxyError)
2020-11-05 05:18:59 +01:00
}
defer proxy.Close()
// Forward request
proxy.Write([]byte(request.String()))
2020-11-05 21:57:28 +01:00
proxy.Write([]byte(newLine))
2020-11-05 05:18:59 +01:00
// Forward response
io.Copy(c, proxy)
2020-11-12 18:56:59 +01:00
return statusSuccess
2020-11-05 05:18:59 +01:00
}