This commit is contained in:
qowevisa 2024-02-23 16:14:35 +02:00
parent 10ed3f21fc
commit 33cf8582e9
3 changed files with 8 additions and 42 deletions

View File

@ -4,9 +4,6 @@ import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
// "fmt"
"io"
"log" "log"
"os" "os"
@ -14,34 +11,22 @@ import (
) )
func main() { func main() {
cert, err := tls.LoadX509KeyPair("tls.crt", "tls.key")
if err != nil {
log.Fatalf("server: loadkeys: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
// config.Rand = rand.Reader
url := fmt.Sprintf("chat.qowevisa.me:%d", env.ConnectPort) url := fmt.Sprintf("chat.qowevisa.me:%d", env.ConnectPort)
// Dial a TLS connection conn, err := tls.Dial("tcp", url, &tls.Config{
conn, err := tls.Dial("tcp", url, &config) InsecureSkipVerify: false, // Set to true if using self-signed certificates
})
if err != nil { if err != nil {
log.Fatalf("Failed to dial: %v", err) log.Fatalf("Failed to dial: %v", err)
} }
defer conn.Close() defer conn.Close()
// Buffer to hold data read from the connection
// buf := make([]byte, 1024) // Adjust size as needed
reader := bufio.NewScanner(os.Stdin) reader := bufio.NewScanner(os.Stdin)
for reader.Scan() { for reader.Scan() {
text := reader.Text() text := reader.Text()
// Read from the connection
_, err := conn.Write([]byte(text + "\n")) _, err := conn.Write([]byte(text + "\n"))
if err != nil { if err != nil {
if err != io.EOF { log.Printf("Write error: %v", err)
log.Printf("Read error: %v", err)
}
break break
} }
// fmt.Printf("Received: %s\n", string(buf[:n]))
} }
} }

View File

@ -2,8 +2,6 @@ package main
import ( import (
"bufio" "bufio"
"crypto/rand"
"crypto/tls"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -13,49 +11,33 @@ import (
) )
func main() { func main() {
// Listen on TCP port 8080 on all available unicast and anycast IP addresses of the local system. url := fmt.Sprintf("127.0.0.1:%d", env.Port)
cert, err := tls.LoadX509KeyPair("tls.crt", "tls.key") listener, err := net.Listen("tcp", url)
if err != nil {
log.Fatalf("server: loadkeys: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
config.Rand = rand.Reader
url := fmt.Sprintf(":%d", env.Port)
listener, err := tls.Listen("tcp", url, &config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer listener.Close() defer listener.Close()
fmt.Println("Server is listening on port 8080") fmt.Printf("Server is listening on %s\n", url)
for { for {
// Wait for a connection.
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Print(err) log.Print(err)
continue continue
} }
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go handleConnection(conn) go handleConnection(conn)
} }
} }
func handleConnection(conn net.Conn) { func handleConnection(conn net.Conn) {
defer conn.Close() defer conn.Close()
fmt.Printf("Client connected: %v\n", conn.RemoteAddr()) fmt.Printf("Client connected: %v\n", conn.RemoteAddr())
// Create a new reader for each client.
scanner := bufio.NewScanner(conn) scanner := bufio.NewScanner(conn)
for scanner.Scan() { for scanner.Scan() {
// Read the incoming connection into the buffer.
text := scanner.Text() text := scanner.Text()
fmt.Printf("Received: %s\n", text) fmt.Printf("Received: %s\n", text)
// Send a response back to client.
_, err := conn.Write([]byte("Message received: " + text + "\n")) _, err := conn.Write([]byte("Message received: " + text + "\n"))
if err != nil { if err != nil {
log.Printf("Failed to write to connection: %v", err) log.Printf("Failed to write to connection: %v", err)
@ -66,6 +48,5 @@ func handleConnection(conn net.Conn) {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading from connection: %s\n", err) fmt.Fprintf(os.Stderr, "Error reading from connection: %s\n", err)
} }
fmt.Printf("Client disconnected: %v\n", conn.RemoteAddr()) fmt.Printf("Client disconnected: %v\n", conn.RemoteAddr())
} }

2
env/env.go vendored
View File

@ -2,5 +2,5 @@ package env
const ( const (
Port = 2993 Port = 2993
ConnectPort = 1993 ConnectPort = 443
) )