Return internal errors to clients

This commit is contained in:
NXShock 2025-06-03 10:36:09 +05:00
parent 1fe5a70c7c
commit c4c9bff7c6
2 changed files with 24 additions and 16 deletions

View file

@ -11,13 +11,13 @@ sha256sums=('SKIP')
backup=("etc/$pkgname.conf") backup=("etc/$pkgname.conf")
build() { build() {
cd "$pkgname" cd $pkgname
go build -o "$pkgname" -ldflags "-linkmode=external -s -w" -buildmode=pie -trimpath -mod=readonly -modcacherw go build -o $pkgname -ldflags "-linkmode=external -s -w" -buildmode=pie -trimpath -mod=readonly -modcacherw
} }
package() { package() {
cd "$pkgname" cd "$pkgname"
install -Dm755 "$pkgname" "$pkgdir"/usr/bin/$pkgname install -Dm755 $pkgname $pkgdir/usr/bin/$pkgname
install -Dm644 "$pkgname.conf" "$pkgdir/etc/$pkgname.conf" install -Dm644 $pkgname.conf $pkgdir/etc/$pkgname.conf
install -Dm755 $pkgname.service "$pkgdir"/usr/lib/systemd/system/$pkgname.service install -Dm755 $pkgname.service $pkgdir/usr/lib/systemd/system/$pkgname.service
} }

View file

@ -30,7 +30,7 @@ func (h HostMapping) Add(host, outputUrlStr string) error {
switch outputUrl.Scheme { switch outputUrl.Scheme {
case "file": case "file":
server := http.Server{Handler: http.FileServer(http.Dir(outputUrl.Path))} server := http.Server{Handler: http.FileServer(http.Dir(outputUrl.Path))}
go func() { _ = server.Serve(pd.listener) }() go server.Serve(pd.listener)
case "tcp": case "tcp":
go func(pd ProxyDirection) { go func(pd ProxyDirection) {
for { for {
@ -39,7 +39,7 @@ func (h HostMapping) Add(host, outputUrlStr string) error {
slog.Debug(err.Error()) slog.Debug(err.Error())
continue continue
} }
go func() { _ = handleTcp(conn.(*tls.Conn), pd.Output) }() go handleTcp(conn.(*tls.Conn), pd.Output)
} }
}(pd) }(pd)
case "unix": case "unix":
@ -50,7 +50,7 @@ func (h HostMapping) Add(host, outputUrlStr string) error {
slog.Debug(err.Error()) slog.Debug(err.Error())
continue continue
} }
go func() { _ = handleUnix(conn.(*tls.Conn), pd.Output) }() go handleUnix(conn.(*tls.Conn), pd.Output)
} }
}(pd) }(pd)
default: default:
@ -79,10 +79,14 @@ func handleTlsConn(conn *tls.Conn, hosts HostMapping) error {
return nil return nil
} }
func handleTcp(conn *tls.Conn, outputUrl *url.URL) error { func handleTcp(conn *tls.Conn, outputUrl *url.URL) {
slog.Debug(fmt.Sprintf("%s -> %s", conn.RemoteAddr(), outputUrl.Host))
c, err := net.Dial(outputUrl.Scheme, outputUrl.Host) c, err := net.Dial(outputUrl.Scheme, outputUrl.Host)
if err != nil { if err != nil {
return fmt.Errorf("dial: %v", err) writeError(conn, err)
conn.Close()
return
} }
defer c.Close() defer c.Close()
@ -102,14 +106,16 @@ func handleTcp(conn *tls.Conn, outputUrl *url.URL) error {
}() }()
wg.Wait() wg.Wait()
return nil
} }
func handleUnix(conn *tls.Conn, outputUrl *url.URL) error { func handleUnix(conn *tls.Conn, outputUrl *url.URL) {
slog.Debug(fmt.Sprintf("%s -> %s", conn.RemoteAddr(), outputUrl.Host+outputUrl.Path))
c, err := net.Dial(outputUrl.Scheme, outputUrl.Host+outputUrl.Path) c, err := net.Dial(outputUrl.Scheme, outputUrl.Host+outputUrl.Path)
if err != nil { if err != nil {
return fmt.Errorf("dial: %v", err) writeError(conn, err)
conn.Close()
return
} }
defer c.Close() defer c.Close()
@ -129,6 +135,8 @@ func handleUnix(conn *tls.Conn, outputUrl *url.URL) error {
}() }()
wg.Wait() wg.Wait()
}
return nil
func writeError(w io.Writer, err error) {
fmt.Fprintf(w, "HTTP/1.1 500 Internal Server Error\r\nConnection: Close\r\nContent-Type: text/plain\r\n\r\n%s", err)
} }