Skip to content

Instantly share code, notes, and snippets.

@sparkoo
Created January 31, 2023 14:58
Show Gist options
  • Save sparkoo/23bb8c5a5a58470b42c1c921e9e1abbf to your computer and use it in GitHub Desktop.
Save sparkoo/23bb8c5a5a58470b42c1c921e9e1abbf to your computer and use it in GitHub Desktop.
passthrough proxy
apiVersion: apps/v1
kind: Deployment
metadata:
name: testproxy
namespace: spi-system
labels:
app.kubernetes.io/name: testproxy
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: testproxy
template:
metadata:
labels:
app.kubernetes.io/name: testproxy
spec:
containers:
- name: testproxy
image: quay.io/mvala/testproxy:latest
imagePullPolicy: Always
command:
- /main
args: ["-upstream", "https://api.cluster-4fxm9.4fxm9.sandbox1216.opentlc.com:6443/"]
ports:
- containerPort: 8080
name: testproxy-port
---
apiVersion: v1
kind: Service
metadata:
name: testproxy
namespace: spi-system
spec:
selector:
app.kubernetes.io/name: testproxy
ports:
- protocol: TCP
port: 8080
targetPort: 8080
name: testproxy
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: testproxy
namespace: spi-system
spec:
port:
targetPort: 8080
to:
kind: Service
name: testproxy
tls:
termination: edge
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
)
type conf struct {
listen string
upstream string
}
func main() {
conf := conf{}
flag.StringVar(&conf.listen, "listen", ":8080", "")
flag.StringVar(&conf.upstream, "upstream", "", "")
flag.Parse()
url, err := url.Parse(conf.upstream)
if err != nil {
panic(err)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("REQ => %+v\n", r)
r.RequestURI = ""
r.URL, _ = url.Parse(fmt.Sprintf("%s://%s%s", url.Scheme, url.Host, r.URL.Path))
response, err := client.Do(r)
if err != nil {
log.Fatal("failed doing request", err)
w.Write([]byte(err.Error()))
return
}
// log.Printf("RESP => %+v\n", response)
for hk, hvl := range response.Header {
for _, hv := range hvl {
// log.Printf("HEADER => '%s' = '%s'\n", hk, hv)
w.Header().Set(hk, hv)
}
}
w.WriteHeader(response.StatusCode)
io.Copy(w, response.Body)
response.Body.Close()
})
server := &http.Server{
Addr: conf.listen,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
server.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment