Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
13e91137
Commit
13e91137
authored
Nov 15, 2024
by
Brad Davidson
Committed by
Brad Davidson
Dec 06, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move http/socks proxy stuff to separate file
Signed-off-by:
Brad Davidson
<
brad.davidson@rancher.com
>
parent
f2f57b4a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
61 deletions
+70
-61
httpproxy.go
pkg/agent/loadbalancer/httpproxy.go
+70
-0
httpproxy_test.go
pkg/agent/loadbalancer/httpproxy_test.go
+0
-0
servers.go
pkg/agent/loadbalancer/servers.go
+0
-61
No files found.
pkg/agent/loadbalancer/httpproxy.go
0 → 100644
View file @
13e91137
package
loadbalancer
import
(
"fmt"
"net"
"net/url"
"os"
"strconv"
"time"
"github.com/k3s-io/k3s/pkg/version"
http_dialer
"github.com/mwitkow/go-http-dialer"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/http/httpproxy"
"golang.org/x/net/proxy"
)
var
defaultDialer
proxy
.
Dialer
=
&
net
.
Dialer
{
Timeout
:
10
*
time
.
Second
,
KeepAlive
:
30
*
time
.
Second
,
}
// SetHTTPProxy configures a proxy-enabled dialer to be used for all loadbalancer connections,
// if the agent has been configured to allow use of a HTTP proxy, and the environment has been configured
// to indicate use of a HTTP proxy for the server URL.
func
SetHTTPProxy
(
address
string
)
error
{
// Check if env variable for proxy is set
if
useProxy
,
_
:=
strconv
.
ParseBool
(
os
.
Getenv
(
version
.
ProgramUpper
+
"_AGENT_HTTP_PROXY_ALLOWED"
));
!
useProxy
||
address
==
""
{
return
nil
}
serverURL
,
err
:=
url
.
Parse
(
address
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to parse address %s"
,
address
)
}
// Call this directly instead of using the cached environment used by http.ProxyFromEnvironment to allow for testing
proxyFromEnvironment
:=
httpproxy
.
FromEnvironment
()
.
ProxyFunc
()
proxyURL
,
err
:=
proxyFromEnvironment
(
serverURL
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to get proxy for address %s"
,
address
)
}
if
proxyURL
==
nil
{
logrus
.
Debug
(
version
.
ProgramUpper
+
"_AGENT_HTTP_PROXY_ALLOWED is true but no proxy is configured for URL "
+
serverURL
.
String
())
return
nil
}
dialer
,
err
:=
proxyDialer
(
proxyURL
,
defaultDialer
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to create proxy dialer for %s"
,
proxyURL
)
}
defaultDialer
=
dialer
logrus
.
Debugf
(
"Using proxy %s for agent connection to %s"
,
proxyURL
,
serverURL
)
return
nil
}
// proxyDialer creates a new proxy.Dialer that routes connections through the specified proxy.
func
proxyDialer
(
proxyURL
*
url
.
URL
,
forward
proxy
.
Dialer
)
(
proxy
.
Dialer
,
error
)
{
if
proxyURL
.
Scheme
==
"http"
||
proxyURL
.
Scheme
==
"https"
{
// Create a new HTTP proxy dialer
httpProxyDialer
:=
http_dialer
.
New
(
proxyURL
,
http_dialer
.
WithDialer
(
forward
.
(
*
net
.
Dialer
)))
return
httpProxyDialer
,
nil
}
else
if
proxyURL
.
Scheme
==
"socks5"
{
// For SOCKS5 proxies, use the proxy package's FromURL
return
proxy
.
FromURL
(
proxyURL
,
forward
)
}
return
nil
,
fmt
.
Errorf
(
"unsupported proxy scheme: %s"
,
proxyURL
.
Scheme
)
}
pkg/agent/loadbalancer/
servers
_test.go
→
pkg/agent/loadbalancer/
httpproxy
_test.go
View file @
13e91137
File moved
pkg/agent/loadbalancer/servers.go
View file @
13e91137
...
@@ -2,66 +2,18 @@ package loadbalancer
...
@@ -2,66 +2,18 @@ package loadbalancer
import
(
import
(
"context"
"context"
"fmt"
"math/rand"
"math/rand"
"net"
"net"
"net/url"
"os"
"slices"
"slices"
"strconv"
"time"
"time"
"github.com/k3s-io/k3s/pkg/version"
http_dialer
"github.com/mwitkow/go-http-dialer"
"github.com/pkg/errors"
"github.com/pkg/errors"
"golang.org/x/net/http/httpproxy"
"golang.org/x/net/proxy"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/wait"
)
)
var
defaultDialer
proxy
.
Dialer
=
&
net
.
Dialer
{
Timeout
:
10
*
time
.
Second
,
KeepAlive
:
30
*
time
.
Second
,
}
// SetHTTPProxy configures a proxy-enabled dialer to be used for all loadbalancer connections,
// if the agent has been configured to allow use of a HTTP proxy, and the environment has been configured
// to indicate use of a HTTP proxy for the server URL.
func
SetHTTPProxy
(
address
string
)
error
{
// Check if env variable for proxy is set
if
useProxy
,
_
:=
strconv
.
ParseBool
(
os
.
Getenv
(
version
.
ProgramUpper
+
"_AGENT_HTTP_PROXY_ALLOWED"
));
!
useProxy
||
address
==
""
{
return
nil
}
serverURL
,
err
:=
url
.
Parse
(
address
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to parse address %s"
,
address
)
}
// Call this directly instead of using the cached environment used by http.ProxyFromEnvironment to allow for testing
proxyFromEnvironment
:=
httpproxy
.
FromEnvironment
()
.
ProxyFunc
()
proxyURL
,
err
:=
proxyFromEnvironment
(
serverURL
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to get proxy for address %s"
,
address
)
}
if
proxyURL
==
nil
{
logrus
.
Debug
(
version
.
ProgramUpper
+
"_AGENT_HTTP_PROXY_ALLOWED is true but no proxy is configured for URL "
+
serverURL
.
String
())
return
nil
}
dialer
,
err
:=
proxyDialer
(
proxyURL
,
defaultDialer
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to create proxy dialer for %s"
,
proxyURL
)
}
defaultDialer
=
dialer
logrus
.
Debugf
(
"Using proxy %s for agent connection to %s"
,
proxyURL
,
serverURL
)
return
nil
}
func
(
lb
*
LoadBalancer
)
setServers
(
serverAddresses
[]
string
)
bool
{
func
(
lb
*
LoadBalancer
)
setServers
(
serverAddresses
[]
string
)
bool
{
serverAddresses
,
hasDefaultServer
:=
sortServers
(
serverAddresses
,
lb
.
defaultServerAddress
)
serverAddresses
,
hasDefaultServer
:=
sortServers
(
serverAddresses
,
lb
.
defaultServerAddress
)
if
len
(
serverAddresses
)
==
0
{
if
len
(
serverAddresses
)
==
0
{
...
@@ -174,19 +126,6 @@ func (s *server) dialContext(ctx context.Context, network, address string) (net.
...
@@ -174,19 +126,6 @@ func (s *server) dialContext(ctx context.Context, network, address string) (net.
return
wrappedConn
,
nil
return
wrappedConn
,
nil
}
}
// proxyDialer creates a new proxy.Dialer that routes connections through the specified proxy.
func
proxyDialer
(
proxyURL
*
url
.
URL
,
forward
proxy
.
Dialer
)
(
proxy
.
Dialer
,
error
)
{
if
proxyURL
.
Scheme
==
"http"
||
proxyURL
.
Scheme
==
"https"
{
// Create a new HTTP proxy dialer
httpProxyDialer
:=
http_dialer
.
New
(
proxyURL
,
http_dialer
.
WithDialer
(
forward
.
(
*
net
.
Dialer
)))
return
httpProxyDialer
,
nil
}
else
if
proxyURL
.
Scheme
==
"socks5"
{
// For SOCKS5 proxies, use the proxy package's FromURL
return
proxy
.
FromURL
(
proxyURL
,
forward
)
}
return
nil
,
fmt
.
Errorf
(
"unsupported proxy scheme: %s"
,
proxyURL
.
Scheme
)
}
// closeAll closes all connections to the server, and removes their entries from the map
// closeAll closes all connections to the server, and removes their entries from the map
func
(
s
*
server
)
closeAll
()
{
func
(
s
*
server
)
closeAll
()
{
s
.
mutex
.
Lock
()
s
.
mutex
.
Lock
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment