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"
"crypto/tls"
"fmt"
// "fmt"
"io"
"log"
"os"
@ -14,34 +11,22 @@ import (
)
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)
// Dial a TLS connection
conn, err := tls.Dial("tcp", url, &config)
conn, err := tls.Dial("tcp", url, &tls.Config{
InsecureSkipVerify: false, // Set to true if using self-signed certificates
})
if err != nil {
log.Fatalf("Failed to dial: %v", err)
}
defer conn.Close()
// Buffer to hold data read from the connection
// buf := make([]byte, 1024) // Adjust size as needed
reader := bufio.NewScanner(os.Stdin)
for reader.Scan() {
text := reader.Text()
// Read from the connection
_, err := conn.Write([]byte(text + "\n"))
if err != nil {
if err != io.EOF {
log.Printf("Read error: %v", err)
}
log.Printf("Write error: %v", err)
break
}
// fmt.Printf("Received: %s\n", string(buf[:n]))
}
}

View File

@ -2,8 +2,6 @@ package main
import (
"bufio"
"crypto/rand"
"crypto/tls"
"fmt"
"log"
"net"
@ -13,49 +11,33 @@ import (
)
func main() {
// Listen on TCP port 8080 on all available unicast and anycast IP addresses of the local system.
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(":%d", env.Port)
listener, err := tls.Listen("tcp", url, &config)
url := fmt.Sprintf("127.0.0.1:%d", env.Port)
listener, err := net.Listen("tcp", url)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
fmt.Println("Server is listening on port 8080")
fmt.Printf("Server is listening on %s\n", url)
for {
// Wait for a connection.
conn, err := listener.Accept()
if err != nil {
log.Print(err)
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)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
fmt.Printf("Client connected: %v\n", conn.RemoteAddr())
// Create a new reader for each client.
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
// Read the incoming connection into the buffer.
text := scanner.Text()
fmt.Printf("Received: %s\n", text)
// Send a response back to client.
_, err := conn.Write([]byte("Message received: " + text + "\n"))
if err != nil {
log.Printf("Failed to write to connection: %v", err)
@ -66,6 +48,5 @@ func handleConnection(conn net.Conn) {
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading from connection: %s\n", err)
}
fmt.Printf("Client disconnected: %v\n", conn.RemoteAddr())
}

2
env/env.go vendored
View File

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