-
Notifications
You must be signed in to change notification settings - Fork 156
/
main.go
113 lines (101 loc) · 2.85 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2019 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"net"
"os"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/private/serrors"
)
var (
localX = flag.String("local_x", "",
"local UDP address on network x, in IP:port format (required)")
remoteX = flag.String("remote_x", "",
"remote UDP address on network x, in IP:port format (required)")
localY = flag.String("local_y", "",
"local UDP address on network y, in IP:port format (required)")
remoteY = flag.String("remote_y", "",
"remote UDP address on network y, in IP:port format (required)")
)
func main() {
flag.Parse()
if err := Proxy(*localX, *remoteX, *localY, *remoteY); err != nil {
log.Error("Fatal proxy error", "err", err)
os.Exit(1)
}
}
func Proxy(localX, remoteX, localY, remoteY string) error {
lxAddr, err := net.ResolveUDPAddr("udp", localX)
if err != nil {
return serrors.New("unable to parse local x address", "err", err)
}
rxAddr, err := net.ResolveUDPAddr("udp", remoteX)
if err != nil {
return serrors.New("unable to parse remote x address", "err", err)
}
xConn, err := net.ListenUDP("udp", lxAddr)
if err != nil {
return serrors.New("unable to open x conn", "err", err)
}
lyAddr, err := net.ResolveUDPAddr("udp", localY)
if err != nil {
return serrors.New("unable to parse local y address", "err", err)
}
ryAddr, err := net.ResolveUDPAddr("udp", remoteY)
if err != nil {
return serrors.New("unable to parse remote y address", "err", err)
}
yConn, err := net.ListenUDP("udp", lyAddr)
if err != nil {
return serrors.New("unable to open y conn", "err", err)
}
log.Info(
fmt.Sprintf(
"Redirecting messages received on %v to %v",
xConn.LocalAddr(),
remoteY,
),
)
log.Info(
fmt.Sprintf(
"Redirecting messages received on %v to %v",
yConn.LocalAddr(),
remoteX,
),
)
go func() {
defer log.HandlePanic()
redirect(xConn, yConn, ryAddr)
}()
go func() {
defer log.HandlePanic()
redirect(yConn, xConn, rxAddr)
}()
select {}
}
func redirect(from, to net.PacketConn, toAddr *net.UDPAddr) {
b := make([]byte, 1<<16)
for {
n, _, err := from.ReadFrom(b)
if err != nil {
log.Error("Unable to read from listen conn", "err", err)
}
_, err = to.WriteTo(b[:n], toAddr)
if err != nil {
log.Error("unable to write to destination", "err", err)
}
}
}