Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-mmproxy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hdacloud
go-mmproxy
Commits
2ad5218a
Commit
2ad5218a
authored
6 years ago
by
Konrad Zemek
Browse files
Options
Downloads
Patches
Plain Diff
Add --allowed-subnets option.
parent
1d587405
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.go
+51
-0
51 additions, 0 deletions
main.go
path-prefixes.txt
+1
-0
1 addition, 0 deletions
path-prefixes.txt
with
52 additions
and
0 deletions
main.go
+
51
−
0
View file @
2ad5218a
...
...
@@ -5,6 +5,7 @@
package
main
import
(
"bufio"
"bytes"
"encoding/binary"
"flag"
...
...
@@ -12,6 +13,7 @@ import (
"io"
"log"
"net"
"os"
"strings"
"syscall"
)
...
...
@@ -19,13 +21,17 @@ import (
var
listenAddr
string
var
targetAddr4
string
var
targetAddr6
string
var
allowedSubnetsPath
string
var
mark
int
var
allowedSubnets
[]
*
net
.
IPNet
func
init
()
{
flag
.
StringVar
(
&
listenAddr
,
"l"
,
"0.0.0.0:8443"
,
"Adress the proxy listens on"
)
flag
.
StringVar
(
&
targetAddr4
,
"4"
,
"0.0.0.0:443"
,
"Address to which IPv4 TCP traffic will be forwarded to"
)
flag
.
StringVar
(
&
targetAddr6
,
"6"
,
"[::]:443"
,
"Address to which IPv6 TCP traffic will be forwarded to"
)
flag
.
IntVar
(
&
mark
,
"mark"
,
123
,
"The mark that will be set on outbound packets"
)
flag
.
StringVar
(
&
allowedSubnetsPath
,
"allowed-subnets"
,
""
,
"Path to a file that contains subnets of the proxy servers"
)
}
func
readRemoteAddrPROXYv2
(
conn
net
.
Conn
,
ctrlBuf
[]
byte
)
(
net
.
Addr
,
net
.
Addr
,
[]
byte
,
error
)
{
...
...
@@ -193,9 +199,28 @@ func copyData(dst net.Conn, src net.Conn, ch chan<- error) {
ch
<-
err
}
func
checkOriginAllowed
(
conn
net
.
Conn
)
bool
{
if
len
(
allowedSubnets
)
==
0
{
return
true
}
addr
:=
conn
.
RemoteAddr
()
.
(
*
net
.
TCPAddr
)
for
_
,
ipNet
:=
range
allowedSubnets
{
if
ipNet
.
Contains
(
addr
.
IP
)
{
return
true
}
}
return
false
}
func
handleConnection
(
conn
net
.
Conn
)
{
defer
conn
.
Close
()
if
!
checkOriginAllowed
(
conn
)
{
log
.
Printf
(
"Disallowed connection from %s"
,
conn
.
RemoteAddr
()
.
String
())
return
}
saddr
,
_
,
restBytes
,
err
:=
readRemoteAddr
(
conn
)
if
err
!=
nil
{
log
.
Printf
(
"Failed to parse PROXY data from %s: %s"
,
conn
.
RemoteAddr
()
.
String
(),
err
.
Error
())
...
...
@@ -248,9 +273,35 @@ func handleConnection(conn net.Conn) {
}
}
func
loadAllowedSubnets
()
error
{
file
,
err
:=
os
.
Open
(
allowedSubnetsPath
)
if
err
!=
nil
{
return
err
}
defer
file
.
Close
()
scanner
:=
bufio
.
NewScanner
(
file
)
for
scanner
.
Scan
()
{
_
,
ipNet
,
err
:=
net
.
ParseCIDR
(
scanner
.
Text
())
if
err
!=
nil
{
return
err
}
allowedSubnets
=
append
(
allowedSubnets
,
ipNet
)
}
return
nil
}
func
main
()
{
flag
.
Parse
()
if
allowedSubnetsPath
!=
""
{
if
err
:=
loadAllowedSubnets
();
err
!=
nil
{
log
.
Fatalf
(
"Failed to load allowed subnets file: %s"
,
err
.
Error
())
}
}
ln
,
err
:=
net
.
Listen
(
"tcp"
,
listenAddr
)
if
err
!=
nil
{
log
.
Fatalf
(
"Failed to bind to %s: %s
\n
"
,
listenAddr
,
err
.
Error
())
...
...
This diff is collapsed.
Click to expand it.
path-prefixes.txt
0 → 100644
+
1
−
0
View file @
2ad5218a
205.220.224.0/21
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment