Files
opencatd-open/router/router.go
2025-04-16 18:01:27 +08:00

77 lines
1.6 KiB
Go

package router
import (
"crypto/tls"
"net"
"net/http"
"net/http/httputil"
"opencatd-open/llm/claude"
oai "opencatd-open/llm/openai"
"time"
"github.com/gin-gonic/gin"
)
func HandleProxy(c *gin.Context) {
if c.Request.URL.Path == "/v1/complete" {
claude.ClaudeProxy(c)
return
}
if c.Request.URL.Path == "/v1/audio/transcriptions" {
oai.WhisperProxy(c)
return
}
if c.Request.URL.Path == "/v1/audio/speech" {
oai.SpeechProxy(c)
return
}
if c.Request.URL.Path == "/v1/images/generations" {
oai.DallEProxy(c)
return
}
if c.Request.URL.Path == "/v1/realtime" {
oai.RealTimeProxy(c)
return
}
if c.Request.URL.Path == "/v1/chat/completions" {
ChatHandler(c)
return
}
}
func HandleReverseProxy(c *gin.Context, targetHost string) {
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = targetHost
},
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
req, err := http.NewRequest(c.Request.Method, c.Request.URL.Path, c.Request.Body)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
req.Header = c.Request.Header
proxy.ServeHTTP(c.Writer, req)
return
}