-
-
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 ab126d2
Showing
10 changed files
with
383 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,64 @@ | ||
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 connHandler(callback 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 | ||
} | ||
|
||
settings, err := ParseSettings(c.Request().Body) | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
return callback(conn, settings) | ||
} | ||
} | ||
|
||
func closeHandler() func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
log.Trace("close handler called") | ||
|
||
return nil | ||
} | ||
} |
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.