proxy: add http/socks5 support, use for Jellyfin

can be found in advanced. Currently only used for the main Jellyfin
client.
This commit is contained in:
Harvey Tindall
2023-10-05 11:25:58 +01:00
parent 543f23c8ef
commit 523902f951
9 changed files with 136 additions and 9 deletions
+42
View File
@@ -0,0 +1,42 @@
// Package easyproxy provides a method to quickly create a http.Transport using given proxy details (SOCKS5 or HTTP).
package easyproxy
import (
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
type Protocol int
const (
SOCKS5 Protocol = iota // SOCKS5
HTTP // HTTP
)
// NewTransport returns a http.Transport using the given proxy details. Leave user/pass blank if not needed.
func NewTransport(p Protocol, addr, user, pass string) (*http.Transport, error) {
t := &http.Transport{}
if p == HTTP {
u := &url.URL{
Scheme: "http",
Host: addr,
}
if user != "" && pass != "" {
u.User = url.UserPassword(user, pass)
}
t.Proxy = http.ProxyURL(u)
return t, nil
}
var auth *proxy.Auth = nil
if user != "" && pass != "" {
auth = &proxy.Auth{User: user, Password: pass}
}
dialer, err := proxy.SOCKS5("tcp", addr, auth, proxy.Direct)
if err != nil {
return nil, nil
}
t.Dial = dialer.Dial
return t, nil
}
+5
View File
@@ -0,0 +1,5 @@
module github.com/hrfee/jfa-go/easyproxy
go 1.20
require golang.org/x/net v0.15.0
+2
View File
@@ -0,0 +1,2 @@
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=