feat: log incoming requests to the user's terminal

This commit is contained in:
2025-02-07 18:30:23 +07:00
parent dfc57cf542
commit d5829ffd0d
9 changed files with 105 additions and 5 deletions

View File

@ -7,16 +7,101 @@ import (
"log"
"net"
"net/http"
"os"
"strings"
"tunnel_pls/session"
indexView "tunnel_pls/view/index"
)
type RouteHandler http.HandlerFunc
// Simple Router Struct
type Router struct {
routes map[string]map[string]http.Handler
}
// NewRouter initializes a new router
func NewRouter() *Router {
return &Router{
routes: make(map[string]map[string]http.Handler),
}
}
// Handle registers a route with an http.Handler
func (r *Router) Handle(method, path string, handler http.Handler) {
if _, exists := r.routes[method]; !exists {
r.routes[method] = make(map[string]http.Handler)
}
r.routes[method][path] = handler
}
// HandleFunc registers a route with a function
func (r *Router) HandleFunc(method, path string, handlerFunc func(http.ResponseWriter, *http.Request)) {
r.Handle(method, path, http.HandlerFunc(handlerFunc))
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if methodRoutes, exists := r.routes[req.Method]; exists {
if handler, exists := methodRoutes[req.URL.Path]; exists {
handler.ServeHTTP(w, req)
return
}
}
http.Error(w, "404 Not Found", http.StatusNotFound)
}
type tcpResponseWriter struct {
conn net.Conn
header http.Header
status int
}
func (w *tcpResponseWriter) Header() http.Header {
return w.header
}
func (w *tcpResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
}
func (w *tcpResponseWriter) Write(data []byte) (int, error) {
fmt.Println("here")
resp := fmt.Sprintf("HTTP/1.1 %d %s\r\n", w.status, http.StatusText(w.status))
for k, v := range w.header {
resp += fmt.Sprintf("%s: %s\r\n", k, v[0])
}
resp += "\r\n" + string(data)
return w.conn.Write([]byte(resp))
}
var router = NewRouter()
func Listen() {
server, err := net.Listen("tcp", ":80")
if err != nil {
log.Fatal(err)
return
}
router.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
indexView.Main("Main Page").Render(r.Context(), w)
return
})
router.HandleFunc("GET", "/public/output.css", func(w http.ResponseWriter, r *http.Request) {
open, err := os.Open("public/output.css")
if err != nil {
return
}
data, _ := io.ReadAll(open)
fmt.Fprintf(w, string(data))
})
//fileserver := http.FileServer(http.Dir("./public"))
//router.Handle("/public/", http.StripPrefix("/public", fileserver))
defer server.Close()
log.Println("Listening on :80")
for {
@ -41,6 +126,18 @@ func handleRequest(conn net.Conn) {
return
}
if r.Host == "localhost" {
writer := &tcpResponseWriter{
conn: conn,
header: make(http.Header),
status: http.StatusOK,
}
fmt.Println(r.Pattern)
router.ServeHTTP(writer, r)
return
}
slug := strings.Split(r.Host, ".")[0]
if slug == "" {
fmt.Println("Error parsing slug: ", r.Host)
@ -73,5 +170,5 @@ func handleRequest(conn net.Conn) {
payload := []byte(rawRequest)
host, originPort := session.ParseAddr(conn.RemoteAddr().String())
sshSession.GetForwardedConnection(conn, host, sshSession.Connection, payload, originPort, 80)
sshSession.GetForwardedConnection(conn, host, sshSession.Connection, payload, originPort, 80, r.RequestURI, r.Method, r.Proto)
}

BIN
main

Binary file not shown.

0
public/input.css Normal file
View File

0
public/output.css Normal file
View File

View File

@ -57,7 +57,7 @@ func (s *Session) handleGlobalRequest() {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(portToBind))
log.Printf("Forwarding approved on port: %d", portToBind)
s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to http://%s.tunnl.live", slug)))
s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to http://%s.tunnl.live \r\n", slug)))
req.Reply(true, buf.Bytes())
} else {
s.TunnelType = TCP
@ -70,6 +70,7 @@ func (s *Session) handleGlobalRequest() {
continue
}
s.Listener = listener
s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to tunnl.live:%d \r\n", portToBind)))
go func() {
for {
conn, err := listener.Accept()
@ -255,7 +256,7 @@ func (s *Session) HandleForwardedConnection(conn net.Conn, sshConn *ssh.ServerCo
}()
}
func (s *Session) GetForwardedConnection(conn net.Conn, host string, sshConn *ssh.ServerConn, payload []byte, originPort, port uint32) {
func (s *Session) GetForwardedConnection(conn net.Conn, host string, sshConn *ssh.ServerConn, payload []byte, originPort, port uint32, path, method, proto string) {
defer conn.Close()
channelPayload := createForwardedTCPIPPayload(host, originPort, port)
channel, reqs, err := sshConn.OpenChannel("forwarded-tcpip", channelPayload)
@ -277,6 +278,7 @@ func (s *Session) GetForwardedConnection(conn net.Conn, host string, sshConn *ss
s.ConnChannels[0].Write([]byte("Could not forward request to the tunnel addr\r\n"))
return
} else {
s.ConnChannels[0].Write([]byte(fmt.Sprintf("\033[32m %s -- [%s] \"%s %s %s\" \r\n \033[0m", host, time.Now().Format("02/Jan/2006 15:04:05"), method, path, proto)))
io.Copy(conn, reader)
}
go func() {

View File

@ -50,8 +50,9 @@ func New(conn *ssh.ServerConn, sshChannel <-chan ssh.NewChannel, req <-chan *ssh
func (session *Session) Close() {
session.Done <- true
if session.TunnelType != HTTP {
session.Listener.Close()
}
for _, ch := range session.ConnChannels {
if err := ch.Close(); err != nil {

0
staging.bat Normal file
View File

0
view/index/index.templ Normal file
View File

0
view/layout/layout.templ Normal file
View File