- Reorganize internal packages and overall project structure - Update imports and wiring to match the new layout - Separate HTTP parsing and streaming from the server package - Separate middleware from the server package - Separate session registry from the session package - Move HTTP, HTTPS, and TCP servers to the transport package - Session package no longer starts the TCP server directly - Server package no longer starts HTTP/HTTPS servers on initialization - Forwarder no longer handles accepting TCP requests - Move session details to the types package - HTTP/HTTPS initialization is now the responsibility of main
49 lines
953 B
Go
49 lines
953 B
Go
package transport
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"log"
|
|
"net"
|
|
"tunnel_pls/internal/registry"
|
|
)
|
|
|
|
type https struct {
|
|
httpHandler *httpHandler
|
|
domain string
|
|
port string
|
|
}
|
|
|
|
func NewHTTPSServer(domain, port string, sessionRegistry registry.Registry, redirectTLS bool) Transport {
|
|
return &https{
|
|
httpHandler: newHTTPHandler(sessionRegistry, redirectTLS),
|
|
domain: domain,
|
|
port: port,
|
|
}
|
|
}
|
|
|
|
func (ht *https) Listen() (net.Listener, error) {
|
|
tlsConfig, err := NewTLSConfig(ht.domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tls.Listen("tcp", ":"+ht.port, tlsConfig)
|
|
|
|
}
|
|
func (ht *https) Serve(listener net.Listener) error {
|
|
log.Printf("HTTPS server is starting on port %s", ht.port)
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
if errors.Is(err, net.ErrClosed) {
|
|
return err
|
|
}
|
|
log.Printf("Error accepting connection: %v", err)
|
|
continue
|
|
}
|
|
|
|
go ht.httpHandler.handler(conn, true)
|
|
}
|
|
}
|