-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (109 loc) · 2.21 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
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"github.com/shynuu/gtp-dscp/u32"
"github.com/urfave/cli/v2"
)
func FindInterface(ipV4 string) (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
log.Printf("Error reading interfaces: %+v\n", err.Error())
return "", err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Printf("localAddresses: %+v\n", err.Error())
return "", err
}
for _, a := range addrs {
switch v := a.(type) {
case *net.IPNet:
if v.IP.To4().String() == ipV4 {
return i.Name, nil
}
}
}
}
return "", errors.New("ERROR interface not found")
}
func run(ipv4 string, offset int) {
var dscp []uint8 = []uint8{0x0a, 0x0c, 0x2e}
for _, d := range dscp {
protocols := []u32.Protocol{
&u32.IPV4Header{
Protocol: u32.PROTO_UDP,
Set: &u32.IPV4Fields{
Protocol: true,
},
},
&u32.UDPHeader{
SourcePort: 2152,
DestinationPort: 2152,
Set: &u32.UDPFields{
SourcePort: true,
DestinationPort: true,
},
},
&u32.GTPv1Header{
HeaderOffset: offset,
Version: 1,
ProtocolType: 1,
Set: &u32.GTPv1Fields{
Version: true,
ProtocolType: true,
},
},
&u32.IPV4Header{
DSCP: d,
Set: &u32.IPV4Fields{
DSCP: true,
},
},
}
iface, _ := FindInterface(ipv4)
var m = u32.NewU32(&protocols, d)
fmt.Println(m.BuildMatches())
m.RunIface(iface)
}
}
func main() {
var ipv4 string = ""
var offset int
app := &cli.App{
Name: "gtp-qos",
Usage: "Copy DSCP field of inner packet to outer packet",
Authors: []*cli.Author{
{Name: "Youssouf Drif"},
},
Copyright: "Copyright (c) 2021 Youssouf Drif",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "ipv4",
Usage: "Select the interface",
Destination: &ipv4,
Required: true,
DefaultText: "unset",
},
&cli.IntFlag{
Name: "offset",
Usage: "Set the offset for GTP Header",
Destination: &offset,
Required: true,
DefaultText: "unset",
},
},
Action: func(c *cli.Context) error {
run(ipv4, offset)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}