@@ -3,14 +3,18 @@ package pom
33import (
44 "encoding/xml"
55 "os"
6+ "path"
67 "path/filepath"
78 "slices"
9+ "strings"
810
911 "github.com/samber/lo"
1012 "github.com/samber/lo/mutable"
1113 "golang.org/x/net/html/charset"
1214)
1315
16+ const trueString = "true"
17+
1418type Server struct {
1519 ID string `xml:"id"`
1620 Username string `xml:"username"`
@@ -23,11 +27,23 @@ type Profile struct {
2327 ActiveByDefault bool `xml:"activation>activeByDefault"`
2428}
2529
30+ type Proxy struct {
31+ ID string `xml:"id"`
32+ Active string `xml:"active"`
33+ Protocol string `xml:"protocol"`
34+ Host string `xml:"host"`
35+ Port string `xml:"port"`
36+ Username string `xml:"username"`
37+ Password string `xml:"password"`
38+ NonProxyHosts string `xml:"nonProxyHosts"`
39+ }
40+
2641type settings struct {
2742 LocalRepository string `xml:"localRepository"`
2843 Servers []Server `xml:"servers>server"`
2944 Profiles []Profile `xml:"profiles>profile"`
3045 ActiveProfiles []string `xml:"activeProfiles>activeProfile"`
46+ Proxies []Proxy `xml:"proxies>proxy"`
3147}
3248
3349func (s settings ) effectiveRepositories () []repository {
@@ -48,6 +64,44 @@ func (s settings) effectiveRepositories() []repository {
4864 return resolvePomRepos (s .Servers , pomRepos )
4965}
5066
67+ func (s settings ) effectiveProxies (protocol , hostname string ) []Proxy {
68+ var proxies []Proxy
69+ for _ , proxy := range s .Proxies {
70+ if ! proxy .isActive () || ! strings .EqualFold (proxy .Protocol , protocol ) {
71+ continue
72+ }
73+ if hostname != "" && proxy .isNonProxyHost (hostname ) {
74+ continue
75+ }
76+ proxies = append (proxies , proxy )
77+ }
78+ return proxies
79+ }
80+
81+ func (p Proxy ) isActive () bool {
82+ return p .Active == trueString || p .Active == ""
83+ }
84+
85+ func (p Proxy ) isNonProxyHost (host string ) bool {
86+ if p .NonProxyHosts == "" {
87+ return false
88+ }
89+
90+ hosts := strings .SplitSeq (p .NonProxyHosts , "|" )
91+ for h := range hosts {
92+ h = strings .TrimSpace (h )
93+ if h == "" {
94+ continue
95+ }
96+
97+ matched , err := path .Match (strings .ToLower (h ), strings .ToLower (host ))
98+ if err == nil && matched {
99+ return true
100+ }
101+ }
102+ return false
103+ }
104+
51105func readSettings () settings {
52106 s := settings {}
53107
@@ -82,6 +136,11 @@ func readSettings() settings {
82136 })
83137 // Merge active profiles
84138 s .ActiveProfiles = lo .Uniq (append (s .ActiveProfiles , globalSettings .ActiveProfiles ... ))
139+
140+ // Merge proxies
141+ s .Proxies = lo .UniqBy (append (s .Proxies , globalSettings .Proxies ... ), func (p Proxy ) string {
142+ return p .ID
143+ })
85144 }
86145
87146 return s
@@ -125,4 +184,15 @@ func expandAllEnvPlaceholders(s *settings) {
125184 for i , activeProfile := range s .ActiveProfiles {
126185 s .ActiveProfiles [i ] = evaluateVariable (activeProfile , nil , nil )
127186 }
187+
188+ for i , proxy := range s .Proxies {
189+ s .Proxies [i ].ID = evaluateVariable (proxy .ID , nil , nil )
190+ s .Proxies [i ].Active = evaluateVariable (proxy .Active , nil , nil )
191+ s .Proxies [i ].Protocol = evaluateVariable (proxy .Protocol , nil , nil )
192+ s .Proxies [i ].Host = evaluateVariable (proxy .Host , nil , nil )
193+ s .Proxies [i ].Port = evaluateVariable (proxy .Port , nil , nil )
194+ s .Proxies [i ].Username = evaluateVariable (proxy .Username , nil , nil )
195+ s .Proxies [i ].Password = evaluateVariable (proxy .Password , nil , nil )
196+ s .Proxies [i ].NonProxyHosts = evaluateVariable (proxy .NonProxyHosts , nil , nil )
197+ }
128198}
0 commit comments