Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"crypto/tls"
"fmt"
"net"
"os"
"bytes"
"io"
)
func main() {
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
if err != nil {
panic(err)
}
config := tls.Config{
Certificates: []tls.Certificate{cert},
}
listener, err := tls.Listen("tcp", ":8080", &config)
if err != nil {
panic(err)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("%s\n", err)
continue
}
go worker(conn)
}
}
func worker(conn net.Conn) {
defer conn.Close()
var req []byte
buf := make([]byte, 64)
requestTerminator := []byte{'\r', '\n', '\r', '\n'}
for {
n, err := conn.Read(buf)
if err != nil {
fmt.Printf("%s\n", err)
return
}
req = append(req, buf[:n]...)
if bytes.HasSuffix(req, requestTerminator) {
break
}
}
os.Stdout.Write(req)
fmt.Printf("connecting...")
outConn, err := net.Dial("tcp", "www.google.com:443")
if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("ok\n")
if _, err := conn.Write([]byte("HTTP/1.1 200 Ok\r\nFoo: bar\r\n\r\n")); err != nil {
panic(err)
}
go io.Copy(conn, outConn)
io.Copy(outConn, conn)
}
var certPEM = `
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAINN8XM4BC00MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTYxMjA3MTkwMjUyWhcNMTcxMjA3MTkwMjUyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAnUMmv696aJVYk0FU39lSmUx2fwttCj9TYqdFl8lwm20rWhy/qZ9gqIFX
xrTlbGG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8qyMaMwiWMEpA+2b219fgFHwQ2kUwVHi
v26CQOqJJ7obo6aSzV/kcAD4f54/Qe/hXpMvt7uO+mH7fH2dJU4StN0mAHcnNw6D
4PChroW9/4WPD8DHhPMnJLp0pSTEVY56+mzZ8iECgYAqaxjIUaiuFgTv06MsKOqG
nOWeCz7gtE7EPm/gC0vy3jG3AF/lVBHNKwXwXV60IDGt3ckqr4FTpFiOxNN9RYxk
fStB7F7ceGdjCD14okw0Dv0yysPmnT5yugcyJ5LEKUQR09XPopmjpc49Z/BRT/Mn
nka39STMsu5nNV290NA+YQ==
-----END PRIVATE KEY-----
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.