test?
This commit is contained in:
parent
82e5eff39a
commit
5da1217e55
|
@ -4,9 +4,7 @@ import (
|
|||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/gotell/env"
|
||||
|
@ -15,33 +13,46 @@ import (
|
|||
func main() {
|
||||
host, err := env.GetHost()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
conn := tls.Client(connp, config)
|
||||
io.WriteString(conn, "Hello secure Server")
|
||||
conn.Close()
|
||||
cert, err := os.ReadFile("server.pem")
|
||||
if err != nil {
|
||||
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")
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/gotell/env"
|
||||
)
|
||||
|
@ -20,39 +20,50 @@ func main() {
|
|||
if err != nil {
|
||||
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)
|
||||
l, err := tls.Listen("tcp", fmt.Sprintf("%s:%d", host, port), config)
|
||||
cert, err := tls.LoadX509KeyPair("server.pem", "server.key")
|
||||
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 {
|
||||
conn, err := l.Accept()
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Printf("server: accept: %s", err)
|
||||
break
|
||||
}
|
||||
go func(c net.Conn) {
|
||||
log.Printf("Get connection: %#v\n", c)
|
||||
io.Copy(os.Stdout, c)
|
||||
fmt.Println()
|
||||
c.Close()
|
||||
}(conn)
|
||||
log.Printf("server: accepted from %s", conn.RemoteAddr())
|
||||
go handleClient(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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user