Created
December 7, 2017 04:57
-
-
Save tmiller/a91355accb7422327a670fd0bc6bc5ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"crypto/tls" | |
"log" | |
"net" | |
) | |
func main() { | |
cer, err := tls.LoadX509KeyPair("localhost.crt", "localhost.key") | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
config := &tls.Config{ | |
Certificates: []tls.Certificate{cer}, | |
MinVersion: tls.VersionTLS12, | |
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, | |
PreferServerCipherSuites: true, | |
CipherSuites: []uint16{ | |
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, | |
tls.TLS_RSA_WITH_AES_256_GCM_SHA384, | |
tls.TLS_RSA_WITH_AES_256_CBC_SHA, | |
}, | |
} | |
ln, err := tls.Listen("tcp", ":8080", config) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
defer ln.Close() | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
go handleConnection(conn) | |
} | |
} | |
func handleConnection(conn net.Conn) { | |
defer conn.Close() | |
r := bufio.NewReader(conn) | |
for { | |
msg, err := r.ReadString('\n') | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
println(msg) | |
n, err := conn.Write([]byte("world\n")) | |
if err != nil { | |
log.Println(n, err) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment