build stuff

This commit is contained in:
2025-09-20 18:29:27 -04:00
parent d2b9f273e0
commit 516427dcda
8 changed files with 206 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ package server
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@@ -19,6 +20,16 @@ import (
"reichard.io/conduit/types"
)
type InfoResponse struct {
Tunnels []TunnelInfo `json:"tunnels"`
Version string `json:"version"`
}
type TunnelInfo struct {
Name string `json:"name"`
Target string `json:"target"`
}
type TunnelConnection struct {
*websocket.Conn
name string
@@ -76,16 +87,31 @@ func (s *Server) Start() error {
}
}
func (s *Server) getStatus(w http.ResponseWriter, _ *http.Request) {
func (s *Server) getInfo(w http.ResponseWriter, _ *http.Request) {
// Get Tunnels
var allTunnels []TunnelInfo
s.mu.RLock()
count := len(s.tunnels)
for t, c := range s.tunnels {
allTunnels = append(allTunnels, TunnelInfo{
Name: t,
Target: c.RemoteAddr().String(),
})
}
s.mu.RUnlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
// Create Response
d, err := json.MarshalIndent(InfoResponse{
Tunnels: allTunnels,
Version: config.GetVersion(),
}, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
response := fmt.Sprintf(`{"tunnels": %d}`, count)
_, _ = w.Write([]byte(response))
// Send Response
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(d)
}
func (s *Server) proxyRawConnection(clientConn net.Conn, tunnelConn *TunnelConnection, dataReader io.Reader) {
@@ -212,8 +238,8 @@ func (s *Server) handleAsHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/_conduit/tunnel":
s.createTunnel(w, r)
case "/_conduit/status":
s.getStatus(w, r)
case "/_conduit/info":
s.getInfo(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}