Skip to content

Commit

Permalink
the top Route header should be optionally kept #16
Browse files Browse the repository at this point in the history
  • Loading branch information
stou committed Dec 2, 2024
1 parent b0d38e4 commit 2cb8165
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
31 changes: 22 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package main

import (
"fmt"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
"io"
"net/http"
_ "net/http/pprof"
"os"
"slices"
"strconv"
"strings"
"time"

"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
)

type HostIp struct {
Expand All @@ -22,9 +24,10 @@ type HostIp struct {
}

type ProxyConfig struct {
Name string
DialogTimeout int `yaml:"dialogTimeout,omitempty"`
Listens []struct {
Name string
DialogTimeout int `yaml:"dialogTimeout,omitempty"`
KeepNextHopRoute string `yaml:"keepNextHopRoute,omitempty"`
Listens []struct {
Address string
UDPPort int `yaml:"udp-port,omitempty"`
TCPPort int `yaml:"tcp-port,omitempty"`
Expand Down Expand Up @@ -111,6 +114,16 @@ func loadConfig(fileName string) (*ProxiesConfigure, error) {

}

func toKeepNextHopRoute(s string) bool {

possibleTrueValues := []string{"true", "yes", "1", "on", "t", "y"}

if s == "" {
s = os.Getenv("KEEP_NEXT_HOP_ROUTE")
}
return slices.Contains(possibleTrueValues, strings.ToLower(s))
}

func startProxies(c *cli.Context) error {
config, err := loadConfig(c.String("config"))
if err != nil {
Expand Down Expand Up @@ -158,7 +171,7 @@ func startProxy(config ProxyConfig, preConfigRoute *PreConfigRoute, resolver *Pr
if dialogTimeout <= 0 {
dialogTimeout = getDefaultDialogTimeout()
}
proxy := NewProxy(config.Name, int64(dialogTimeout), preConfigRoute, resolver, selfLearnRoute)
proxy := NewProxy(config.Name, int64(dialogTimeout), toKeepNextHopRoute(config.KeepNextHopRoute), preConfigRoute, resolver, selfLearnRoute)
for _, listen := range config.Listens {
item, err := NewProxyItem(listen.Address,
listen.UDPPort,
Expand Down
9 changes: 7 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (p *MyName) isMyMessage(msg *Message) bool {

type Proxy struct {
myName *MyName
keepNextHopRoute bool
preConfigRoute *PreConfigRoute
resolver *PreConfigHostResolver
items []*ProxyItem
Expand All @@ -131,10 +132,12 @@ type Proxy struct {

func NewProxy(name string,
dialogExpire int64,
keepNextHopRoute bool,
preConfigRoute *PreConfigRoute,
resolver *PreConfigHostResolver,
selfLearnRoute *SelfLearnRoute) *Proxy {
proxy := &Proxy{myName: NewMyName(name),
keepNextHopRoute: keepNextHopRoute,
preConfigRoute: preConfigRoute,
resolver: resolver,
items: make([]*ProxyItem, 0),
Expand Down Expand Up @@ -343,11 +346,11 @@ func (p *Proxy) HandleMessage(msg *Message) {
if err == nil {
zap.L().Info("Get next hop", zap.String("host", host), zap.Int("port", port), zap.String("transport", transport))
serverTrans, ok := p.selfLearnRoute.GetRoute(host)

if ok {
p.addVia(msg, serverTrans)
p.addRecordRoute(msg, serverTrans)
}

p.sendMessage(host, port, transport, msg)
} else if p.myName.isMyMessage(msg) {
zap.L().Info("it is my request")
Expand Down Expand Up @@ -539,7 +542,9 @@ func (P *Proxy) getNextRequestHopByRoute(msg *Message) (host string, port int, t
if err != nil {
return
}
msg.PopRoute()
if !P.keepNextHopRoute {
msg.PopRoute()
}
addr := routeParam.GetAddress().GetAddress()
if addr.IsSIPURI() {
sipUri, _ := addr.GetSIPURI()
Expand Down
4 changes: 2 additions & 2 deletions via.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func (v *Via) String() string {
}

func (vp *ViaParam) SetParam(name string, value string) {
for _, param := range vp.Params {
for key, param := range vp.Params {
if param.Key == name {
param.Value = value
vp.Params[key].Value = value
return
}
}
Expand Down

0 comments on commit 2cb8165

Please sign in to comment.