This repository was archived by the owner on May 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebui.go
More file actions
107 lines (94 loc) · 2.47 KB
/
Copy pathwebui.go
File metadata and controls
107 lines (94 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bufio"
"bytes"
"crypto/tls"
_ "embed"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
//go:embed getcertpage.html
var getCertPageFile []byte
func webuiProcess(r *http.Request) (code int, mimetype string, data []byte) {
file := "getcertpage.html"
mimetype = "text/html"
code = 200
switch r.URL.Path {
case "/cert.pem":
file = certFile
mimetype = "application/x-x509-ca-cert"
case "/cert.cer":
file = certFileCer
mimetype = "application/x-x509-ca-cert"
case "/client.p12":
file = clientIdentFile
mimetype = "application/x-pkcs12"
case "/client.mobileconfig":
file = clientConfigFile
mimetype = "application/x-apple-aspen-config"
default:
return code, mimetype, getCertPageFile
}
data, err := os.ReadFile(file)
if err != nil {
log.Printf("Error reading cert file: %v", err)
// Send error response to client
code = 500
mimetype = "text/plain"
data = []byte("Internal Server Error")
}
return code, mimetype, data
}
func serveWebUIPlain(w http.ResponseWriter, r *http.Request) {
if *logURLs {
log.Printf("[%s] HTTP URL: %s %s", fmt.Sprintf("%p", r), r.Method, r.URL.String())
}
code, mimetype, data := webuiProcess(r)
w.Header().Add("Content-Type", mimetype)
w.Header().Add("Content-Length", strconv.Itoa(len(data)))
w.WriteHeader(code)
_, err := io.Copy(w, bytes.NewReader(data))
if err != nil {
log.Printf("[%s] Error writing response to client: %v", r.RemoteAddr, err)
return
}
}
func serveWebUITLS(tlsConn *tls.Conn, host, name string, clientHello *clientHelloInfo, connID string) {
// Read HTTP requests from client and forward to server
reader := bufio.NewReader(tlsConn)
// Read the request
req, err := http.ReadRequest(reader)
if err != nil {
if err != io.EOF {
log.Printf("[%s] Error reading request: %v", connID, err)
}
return
}
if *logURLs {
fullURL := fmt.Sprintf("https://%s%s", req.Host, req.URL.Path)
if req.URL.RawQuery != "" {
fullURL += "?" + req.URL.RawQuery
}
log.Printf("[%s] MITM URL: %s %s", connID, req.Method, fullURL)
}
code, mimetype, data := webuiProcess(req)
res := &http.Response{
StatusCode: code,
Body: io.NopCloser(bytes.NewReader(data)),
Header: http.Header{},
ProtoMajor: 1,
}
defer res.Body.Close()
res.Header.Add("Content-Type", mimetype)
res.Header.Add("Content-Length", strconv.Itoa(len(data)))
err = res.Write(tlsConn)
if err != nil {
log.Printf("[%s] Error writing response to client: %v", connID, err)
return
}
tlsConn.Close()
}