-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
67 lines (53 loc) · 1.45 KB
/
http.go
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
package http
import (
"github.com/topvisor/go-prifma/pkg/conf"
"github.com/topvisor/go-prifma/pkg/prifma"
"github.com/topvisor/go-prifma/pkg/utils"
"net/http"
)
const ModuleDirective = "http"
type Http struct {
RoundTrippers RoundTrippersMap
}
func New() *Http {
return &Http{
RoundTrippers: NewSyncRoundTrippersMap(),
}
}
func (t *Http) HandleRequest(result prifma.HandleRequestResult) (prifma.HandleRequestResult, error) {
if result.GetRequest().Method == http.MethodConnect {
return result, nil
}
dialer := result.GetDialer()
host := utils.GetHostname(result.GetRequest().Host)
localIp, err := dialer.GetLocalIp(host)
if err != nil {
if err == prifma.ErrOutgoingIpNotDefined {
result.SetResponse(prifma.NewResponseError(http.StatusBadRequest, err.Error()))
} else {
result.SetResponse(prifma.NewResponseError(http.StatusBadGateway, err.Error()))
}
return result, nil
}
if localIpV4 := localIp.To4(); localIpV4 != nil {
dialer.SetIpV4(localIpV4)
dialer.SetIpV6(nil)
} else {
dialer.SetIpV4(nil)
dialer.SetIpV6(localIp)
}
result.SetResponse(NewResponseReverseProxy(t.RoundTrippers))
return result, nil
}
func (t *Http) GetDirective() string {
return ModuleDirective
}
func (t *Http) Clone() prifma.Module {
return t
}
func (t *Http) Call(command conf.Command) error {
return conf.NewErrCommandName(command)
}
func (t *Http) CallBlock(command conf.Command) (conf.Block, error) {
return nil, conf.NewErrCommandName(command)
}