This commit is contained in:
qowevisa 2024-02-25 03:36:00 +02:00
parent 82e5eff39a
commit 5da1217e55
2 changed files with 75 additions and 53 deletions

View File

@ -4,9 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io"
"log" "log"
"net"
"os" "os"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
@ -15,33 +13,46 @@ import (
func main() { func main() {
host, err := env.GetHost() host, err := env.GetHost()
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
port, err := env.GetPort() port, err := env.GetPort()
if err != nil {
panic(err)
}
//
rootCert, err := os.ReadFile("./server.pem")
if err != nil {
panic(err)
}
//
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(rootCert)
if !ok {
log.Fatal("failed to parse root certificate")
}
config := &tls.Config{RootCAs: roots, ServerName: "my-server"}
log.Printf("Trying to dial %s:%d\n", host, port)
connp, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
conn := tls.Client(connp, config) cert, err := os.ReadFile("server.pem")
io.WriteString(conn, "Hello secure Server") if err != nil {
conn.Close() log.Fatalf("client: load root cert: %s", err)
}
roots := x509.NewCertPool()
if ok := roots.AppendCertsFromPEM(cert); !ok {
log.Fatalf("client: failed to parse root certificate")
}
config := &tls.Config{RootCAs: roots, ServerName: host}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
if err != nil {
log.Fatalf("client: dial: %s", err)
}
defer conn.Close()
log.Println("client: connected to: ", conn.RemoteAddr())
message := "Hello secure Server\n"
n, err := conn.Write([]byte(message))
if err != nil {
log.Fatalf("client: write: %s", err)
}
log.Printf("client: wrote %q (%d bytes)", message, n)
reply := make([]byte, 256)
n, err = conn.Read(reply)
if err != nil {
log.Fatalf("client: read: %s", err)
}
log.Printf("client: read %q (%d bytes)", string(reply[:n]), n)
log.Print("client: exiting")
} }

View File

@ -1,12 +1,12 @@
package main package main
import ( import (
"crypto/rand"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io" "io"
"log" "log"
"net" "net"
"os"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
) )
@ -20,39 +20,50 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
//
serverCert, err := os.ReadFile("./server.pem")
if err != nil {
log.Fatal(err)
}
serverKey, err := os.ReadFile("./server.key")
if err != nil {
log.Fatal(err)
}
cer, err := tls.X509KeyPair(serverCert, serverKey)
if err != nil {
log.Fatal(err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
//
log.Printf("Serving on %s:%d\n", host, port) cert, err := tls.LoadX509KeyPair("server.pem", "server.key")
l, err := tls.Listen("tcp", fmt.Sprintf("%s:%d", host, port), config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("server: loadkeys: %s", err)
} }
defer l.Close() config := tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.NoClientCert}
config.Rand = rand.Reader
service := fmt.Sprintf("%s:%d", host, port)
listener, err := tls.Listen("tcp", service, &config)
if err != nil {
log.Fatalf("server: listen: %s", err)
}
log.Printf("server: listening on %s", service)
for { for {
conn, err := l.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Fatal(err) log.Printf("server: accept: %s", err)
break
} }
go func(c net.Conn) { log.Printf("server: accepted from %s", conn.RemoteAddr())
log.Printf("Get connection: %#v\n", c) go handleClient(conn)
io.Copy(os.Stdout, c)
fmt.Println()
c.Close()
}(conn)
} }
} }
func handleClient(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 512)
for {
log.Print("server: conn: waiting")
n, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("server: conn: read: %s", err)
}
break
}
log.Printf("server: conn: echo %q\n", string(buf[:n]))
_, err = conn.Write(buf[:n])
if err != nil {
log.Printf("server: conn: write: %s", err)
break
}
}
log.Println("server: conn: closed")
}