-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edbdd8f
commit e3b0153
Showing
13 changed files
with
758 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package vpn | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/labstack/echo/v4" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func handler(handler func(net.Conn, *Settings) error) func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
log.Debug("handler started") | ||
defer log.Debug("handler done") | ||
|
||
conn, _, err := c.Response().Hijack() | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
defer conn.Close() | ||
|
||
settings, err := ParseSettings(c.Request().Body) | ||
if err != nil { | ||
log.WithError(err).Error("faild to parse the settings") | ||
|
||
return err | ||
} | ||
|
||
// NOTE: the [handler] is called to handler the core logic of the VPN client, while this handler is used to extract | ||
// the connection and the settings data. | ||
if err := handler(conn, settings); err != nil { | ||
log.WithError(err).Error("failed to handler the vpn connection between server and agent") | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func closeHandler(callback func() error) func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
log.Trace("close handler called") | ||
|
||
return callback() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ifce | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/songgao/water" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// MaximumTransmissionUnit (MTU) is the size of the largest protocol data unit (PDU) that can be communicated in a | ||
// single network layer transaction. | ||
const MaximumTransmissionUnit = 1000 | ||
|
||
const InterfaceName = "shb" | ||
|
||
var ErrGenerateInterface = errors.New("failed to generate an interface") | ||
|
||
func generateInterfaceName() (string, error) { | ||
const attempts = 10 | ||
|
||
for i := 0; i < attempts; i++ { | ||
name := fmt.Sprintf("%s%d", InterfaceName, i) | ||
|
||
if _, err := netlink.LinkByName(name); err == nil { | ||
continue | ||
} | ||
|
||
return name, nil | ||
} | ||
|
||
return "", ErrGenerateInterface | ||
} | ||
|
||
type Interface struct { | ||
face *water.Interface | ||
link netlink.Link | ||
} | ||
|
||
var ( | ||
ErrInterfaceCreate = errors.New("failed to create the interface") | ||
ErrInterfaceUp = errors.New("failed to get up the interface") | ||
ErrInterfaceConfiguration = errors.New("failed to configure the interface") | ||
ErrInterfaceMTU = errors.New("failed to configure the MTU") | ||
) | ||
|
||
func NewInterface(addrr string) (*Interface, error) { | ||
name, err := generateInterfaceName() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
iface, err := water.New(water.Config{ | ||
DeviceType: water.TUN, | ||
PlatformSpecificParams: water.PlatformSpecificParams{ | ||
Name: name, | ||
MultiQueue: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, errors.Join(ErrInterfaceCreate, err) | ||
} | ||
|
||
addr, err := netlink.ParseAddr(addrr) | ||
if err != nil { | ||
return nil, errors.Join(ErrInterfaceConfiguration, err) | ||
} | ||
|
||
link, err := netlink.LinkByName(iface.Name()) | ||
if err != nil { | ||
return nil, errors.Join(ErrInterfaceConfiguration, err) | ||
} | ||
|
||
if err := netlink.AddrAdd(link, addr); err != nil { | ||
return nil, errors.Join(ErrInterfaceConfiguration, err) | ||
} | ||
|
||
if err := netlink.LinkSetMTU(link, MaximumTransmissionUnit); err != nil { | ||
return nil, errors.Join(ErrInterfaceMTU, err) | ||
} | ||
|
||
return &Interface{ | ||
face: iface, | ||
link: link, | ||
}, nil | ||
} | ||
|
||
func (i *Interface) Up() error { | ||
if err := netlink.LinkSetUp(i.link); err != nil { | ||
return errors.Join(ErrInterfaceUp, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Interface) Name() string { | ||
return i.face.Name() | ||
} | ||
|
||
func (i *Interface) Close() error { | ||
return i.face.Close() | ||
} | ||
|
||
func (i *Interface) Read(buffer []byte) (int, error) { | ||
return i.face.Read(buffer) | ||
} | ||
|
||
func (i *Interface) Write(buffer []byte) (int, error) { | ||
return i.face.Write(buffer) | ||
} |
Oops, something went wrong.