Initial commit: unified dashboard suite state with Khira and Image Manipulator 3-file setup

This commit is contained in:
2026-10-26 11:17:13 +05:30
commit 6c60b4ecb8
148 changed files with 128149 additions and 0 deletions
+225
View File
@@ -0,0 +1,225 @@
package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
)
var httpClient = &http.Client{
Timeout: 200 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Allow self-signed
},
}
func proxyRequestHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(512 << 20); err != nil { // 512MB
if err == http.ErrNotMultipart {
if err2 := r.ParseForm(); err2 != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"status_code": 400, "content": "Failed to parse form: " + err2.Error()})
return
}
} else {
writeJSON(w, http.StatusBadRequest, map[string]any{"status_code": 400, "content": "Failed to parse form: " + err.Error()})
return
}
}
targetURL := r.FormValue("url")
method := r.FormValue("method")
if method == "" {
method = "POST"
}
// Parse headers
headersStr := r.FormValue("headers")
var headers map[string]string
if err := json.Unmarshal([]byte(headersStr), &headers); err != nil {
headers = make(map[string]string)
}
// Parse auth
authStr := r.FormValue("auth")
var auth map[string]string
if err := json.Unmarshal([]byte(authStr), &auth); err != nil {
auth = map[string]string{"type": "none"}
}
// Construct request body
var reqBody io.Reader
var reqContentType string
hasFiles := r.MultipartForm != nil && len(r.MultipartForm.File) > 0
if hasFiles {
// Multipart request
bodyBuf := &bytes.Buffer{}
writer := multipart.NewWriter(bodyBuf)
// Form fields
for key, values := range r.MultipartForm.Value {
if isInternalKey(key) {
continue
}
for _, val := range values {
writer.WriteField(key, val)
}
}
// Files
for key, files := range r.MultipartForm.File {
for _, fileHeader := range files {
part, err := writer.CreateFormFile(key, fileHeader.Filename)
if err != nil {
continue
}
file, err := fileHeader.Open()
if err != nil {
continue
}
io.Copy(part, file)
file.Close()
}
}
writer.Close()
reqBody = bodyBuf
reqContentType = writer.FormDataContentType()
} else if r.FormValue("body_type") == "json" {
bodyField := r.FormValue("body")
if bodyField == "" {
bodyField = r.FormValue("_json_body")
}
reqBody = bytes.NewBufferString(bodyField)
if _, ok := headers["Content-Type"]; !ok {
headers["Content-Type"] = "application/json"
}
} else {
// URL encoded form
form := url.Values{}
srcMap := r.Form
if r.MultipartForm != nil {
srcMap = r.MultipartForm.Value
}
for key, values := range srcMap {
if !isInternalKey(key) {
for _, val := range values {
form.Add(key, val)
}
}
}
encoded := form.Encode()
if encoded != "" {
reqBody = bytes.NewBufferString(encoded)
if _, ok := headers["Content-Type"]; !ok {
headers["Content-Type"] = "application/x-www-form-urlencoded"
}
}
}
// Create Request
outReq, err := http.NewRequest(method, targetURL, reqBody)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]any{"status_code": 500, "content": err.Error()})
return
}
// Auth
authType := auth["type"]
if authType == "bearer" {
outReq.Header.Set("Authorization", "Bearer "+auth["token"])
} else if authType == "basic" {
outReq.SetBasicAuth(auth["username"], auth["password"])
} else if authType == "apikey" {
if key, ok := auth["key"]; ok && key != "" {
outReq.Header.Set(key, auth["value"])
}
}
// Set headers
for k, v := range headers {
if hasFiles && strings.ToLower(k) == "content-type" {
continue // skip custom content-type if multipart
}
outReq.Header.Set(k, v)
}
if hasFiles {
outReq.Header.Set("Content-Type", reqContentType)
}
startTime := time.Now()
slog.Info("proxying request", "method", method, "url", targetURL, "has_files", hasFiles)
// Execute
resp, err := httpClient.Do(outReq)
duration := time.Since(startTime).Milliseconds()
if err != nil {
// Timeout or connection error
statusCode := 500
content := "Internal Proxy Error: " + err.Error()
if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() {
statusCode = 408
content = "Request Timeout"
}
slog.Error("proxy request failed", "url", targetURL, "status", statusCode, "duration_ms", duration, "error", err)
writeJSON(w, http.StatusOK, map[string]any{
"status_code": statusCode,
"content": content,
"headers": map[string]string{},
}) // Python returns 200 wrapper
return
}
defer resp.Body.Close()
slog.Info("proxy request completed", "url", targetURL, "status", resp.StatusCode, "duration_ms", duration)
// Parse Response
respData := map[string]any{
"status_code": resp.StatusCode,
"headers": make(map[string]string),
"content": nil,
"binary_data": nil,
"content_type": resp.Header.Get("Content-Type"),
}
for k, v := range resp.Header {
if len(v) > 0 {
respData["headers"].(map[string]string)[k] = v[0]
}
}
bodyBytes, _ := io.ReadAll(resp.Body)
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
isBinary := strings.Contains(contentType, "image") || strings.Contains(contentType, "pdf") ||
strings.Contains(contentType, "octet-stream") || strings.Contains(contentType, "audio") ||
strings.Contains(contentType, "video") || strings.Contains(contentType, "zip")
if isBinary {
respData["binary_data"] = base64.StdEncoding.EncodeToString(bodyBytes)
respData["content"] = fmt.Sprintf("[Binary Data: %d bytes]", len(bodyBytes))
} else {
respData["content"] = string(bodyBytes)
}
writeJSON(w, http.StatusOK, respData)
}
func isInternalKey(key string) bool {
keys := []string{"url", "method", "headers", "auth", "body_type", "body", "_json_body"}
for _, k := range keys {
if k == key {
return true
}
}
return false
}