Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
go-version: stable

- uses: golangci/golangci-lint-action@v6
- uses: golangci/golangci-lint-action@v9
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: "2"

run:
timeout: 5m
modules-download-mode: readonly
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type Config struct {
DisableKeyboardRecord bool `mapstructure:"DISABLE_KEYBOARD_RECORD"`

DriveScope string `mapstructure:"LION_DRIVE_SCOPE"` // user or session
// DOMAINS=* "demo.example.com:443,172.17.200.191:80"
DOMAINS string `mapstructure:"DOMAINS"`
}

func (c *Config) UpdateRedisPassword(val string) {
Expand Down
72 changes: 69 additions & 3 deletions pkg/tunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"io"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"path/filepath"
"strconv"
Expand All @@ -32,13 +34,77 @@
defaultBufferSize = 1024
)

func normalizeHost(h string) string {
h = strings.TrimSpace(strings.ToLower(h))
host, _, err := net.SplitHostPort(h)
if err == nil {
return strings.Trim(host, "[]")
}
return strings.Trim(h, "[]")
}

func hostMatches(allowedHost, originHost string) bool {
allowedName := normalizeHost(allowedHost)
originName := normalizeHost(originHost)
if allowedName == "" || originName == "" {
return false
}
return allowedName == originName
}

func isInternalHost(host string) bool {
host = normalizeHost(host)
if host == "localhost" {
return true
}

addr, err := netip.ParseAddr(host)
if err != nil {
return false
}
return addr.IsLoopback() || addr.IsPrivate() || addr.IsLinkLocalUnicast()
}

func domainsAllowOrigin(originHost, domains string) bool {
for _, domain := range strings.Split(domains, ",") {
domain = strings.TrimSpace(domain)
if domain == "" {
continue
}
if domain == "*" || hostMatches(domain, originHost) {
return true
}
}
return false
}

func checkOrigin(r *http.Request) bool {
origin := r.Header.Get("Origin")
// 允许非浏览器,客户端访问
if len(origin) == 0 {
return true
}
u, err := url.Parse(origin)
if err != nil {
return false
}
originHost := normalizeHost(u.Host)
reqHost := normalizeHost(r.Host)
if hostMatches(originHost, reqHost) {
return true
}
if isInternalHost(originHost) {
return true
}

return domainsAllowOrigin(originHost, config.GlobalConfig.DOMAINS)
}

var upGrader = websocket.Upgrader{
ReadBufferSize: defaultBufferSize,
WriteBufferSize: defaultBufferSize,
Subprotocols: []string{"guacamole"},
CheckOrigin: func(r *http.Request) bool {
return true
},
CheckOrigin: checkOrigin,
}

type GuacamoleTunnelServer struct {
Expand Down Expand Up @@ -81,7 +147,7 @@
ctx.AbortWithStatus(http.StatusBadRequest)
return
}
defer ws.Close()

Check failure on line 150 in pkg/tunnel/server.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ws.Close` is not checked (errcheck)

tokenId, ok := ctx.GetQuery("TOKEN_ID")
if !ok {
Expand Down Expand Up @@ -206,7 +272,7 @@
g.RecordLifecycleLog(sessionId, model.AssetConnectFinished, reason)
return
}
defer tunnel.Close()

Check failure on line 275 in pkg/tunnel/server.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `tunnel.Close` is not checked (errcheck)
g.RecordLifecycleLog(sessionId, model.AssetConnectSuccess, model.EmptyLifecycleLog)

logger.Infof("Session[%s] use resolution (%d*%d)",
Expand Down Expand Up @@ -403,7 +469,7 @@
ctx.AbortWithStatus(http.StatusBadRequest)
return
}
defer ws.Close()

Check failure on line 472 in pkg/tunnel/server.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ws.Close` is not checked (errcheck)
userItem, ok := ctx.Get(config.GinCtxUserKey)
if !ok {
_ = ws.WriteMessage(websocket.TextMessage, []byte(ErrAuthUser.String()))
Expand Down Expand Up @@ -439,7 +505,7 @@
_ = ws.WriteMessage(websocket.TextMessage, []byte(ErrNoSession.String()))
return
}
defer tunnelCon.Close()

Check failure on line 508 in pkg/tunnel/server.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `tunnelCon.Close` is not checked (errcheck)

ints := guacd.NewInstruction(INTERNALDATAOPCODE, tunnelCon.UUID())
_ = ws.WriteMessage(websocket.TextMessage, []byte(ints.String()))
Expand Down
Loading