-
-
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 73ad9bc
Showing
10 changed files
with
428 additions
and
10 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,74 @@ | ||
package vpn | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/labstack/echo/v4" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// PacketMaxSize defines the max size of each IP packet received. | ||
// TODO: This isn't the real max size; set the right one. | ||
const PacketMaxSize = 2048 | ||
|
||
// InterfaceName defines the default name for the network interface created by ShellHub. | ||
const InterfaceName string = "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 | ||
} | ||
|
||
func handler(handler func(net.Conn, *Settings) error) func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
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,35 @@ | ||
package vpn | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
) | ||
|
||
type Settings struct { | ||
Address [4]byte `json:"address"` | ||
Mask byte `json:"mask"` | ||
} | ||
|
||
// ParseSettings read and parses the [Settings] structure from an [io.Reader]. | ||
func ParseSettings(data io.Reader) (*Settings, error) { | ||
body, err := io.ReadAll(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
settings := Settings{} | ||
if err = json.Unmarshal(body, &settings); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &settings, nil | ||
} | ||
|
||
// String converts a [Settings] to a string representation on the format $IP/$Mask. | ||
func (s *Settings) String() string { | ||
ip := net.IPv4(s.Address[0], s.Address[1], s.Address[2], s.Address[3]) | ||
|
||
return fmt.Sprintf("%s/%d", ip.String(), s.Mask) | ||
} |
Oops, something went wrong.