This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
46 changed files
with
5,253 additions
and
521 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
inpackage: True | ||
with-expecter: True | ||
testonly: True | ||
include-auto-generated: False |
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 |
---|---|---|
|
@@ -27,6 +27,11 @@ update: | |
@go get -u all | ||
go mod tidy | ||
|
||
.PHONY: generate | ||
generate: | ||
@go install github.com/vektra/mockery/[email protected] | ||
mockery | ||
|
||
.PHONY: models | ||
models: | ||
@go install github.com/volatiletech/sqlboiler/v4@latest | ||
|
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,83 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ClientPolicy struct { | ||
ClientRHID string | ||
SessionDuration time.Duration // Session Duration: minutes | ||
UploadRateThresholdKBs int64 // Upload Rate Limit Threshold: Kb/s | ||
DownloadRateThresholdKBs int64 // Download Rate Limit Threshold: Kb/s | ||
UploadQuotaKB int64 // Upload Quota: KBytes | ||
DownloadQuotaKB int64 // Download Quota: KBytes | ||
|
||
Custom map[string]any // Custom values | ||
} | ||
|
||
func (c *ClientPolicy) toAuthAttemptLog() { | ||
// TODO: implement | ||
panic("not implemented") | ||
} | ||
|
||
// toOpenNDSFormat converts ClientPolicy to the format that is expected by opennds auth daemon ("authmon") | ||
func (c *ClientPolicy) toOpenNDSFormat() string { | ||
custom := "" | ||
if c.Custom != nil && len(c.Custom) > 0 { | ||
idx := 0 | ||
kvFormats := make([]string, len(c.Custom)) | ||
|
||
for k, v := range c.Custom { | ||
kvFormats[idx] = fmt.Sprintf("%s=%s", k, v) | ||
idx += 1 | ||
} | ||
|
||
custom = strings.Join(kvFormats, ", ") | ||
} | ||
|
||
format := fmt.Sprintf( | ||
"%s %d %d %d %d %d %s", | ||
c.ClientRHID, | ||
int64(c.SessionDuration.Minutes()), | ||
c.UploadRateThresholdKBs, | ||
c.DownloadRateThresholdKBs, | ||
c.UploadQuotaKB, | ||
c.DownloadQuotaKB, | ||
base64.StdEncoding.EncodeToString([]byte(custom)), | ||
) | ||
|
||
format = url.QueryEscape(format) | ||
// NOTE(@Kcrong): url.QueryEscape replaces spaces with "+", but opennds needs "%20" | ||
format = strings.ReplaceAll(format, "+", "%20") | ||
|
||
return format | ||
} | ||
|
||
func (c *ClientPolicy) AddTag(key string, value any) { | ||
c.Custom[key] = value | ||
} | ||
|
||
func NewClientPolicy( | ||
rhid string, | ||
durationMinutes int64, | ||
uploadRate, | ||
downloadRate, | ||
uploadQuota, | ||
downloadQuota int64, | ||
tags map[string]any, | ||
) *ClientPolicy { | ||
|
||
return &ClientPolicy{ | ||
ClientRHID: rhid, | ||
SessionDuration: time.Duration(durationMinutes) * time.Minute, | ||
UploadRateThresholdKBs: uploadRate, | ||
DownloadRateThresholdKBs: downloadRate, | ||
UploadQuotaKB: uploadQuota, | ||
DownloadQuotaKB: downloadQuota, | ||
Custom: tags, | ||
} | ||
} |
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,55 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
// Provider is an interface for client(RHID) authentication | ||
type Provider interface { | ||
ListClients(ctx context.Context, gateway string) ([]string, error) | ||
AddClient(ctx context.Context, gateway, rhid string, policy *ClientPolicy) error | ||
DeleteClient(ctx context.Context, gateway, rhid string) error | ||
} | ||
|
||
type provider struct { | ||
redisCli *redis.Client | ||
} | ||
|
||
func hash(s string) string { | ||
bs := sha256.Sum256([]byte(s)) | ||
|
||
return hex.EncodeToString(bs[:]) | ||
} | ||
|
||
func (p provider) ListClients(ctx context.Context, gateway string) ([]string, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (p provider) AddClient(ctx context.Context, gateway, rhid string, policy *ClientPolicy) error { | ||
gwHash := hash(gateway) | ||
if err := p.redisCli.LPush(ctx, gwHash, rhid).Err(); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
key := fmt.Sprintf("%s-%s", gwHash, rhid) | ||
if err := p.redisCli.Set(ctx, key, policy.toOpenNDSFormat(), 0).Err(); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p provider) DeleteClient(ctx context.Context, gateway, rhid string) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func NewProvider(redisCli *redis.Client) Provider { | ||
return &provider{redisCli: redisCli} | ||
} |
Oops, something went wrong.