-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from beeper/tulir/relay-mode
Add relay mode
- Loading branch information
Showing
8 changed files
with
406 additions
and
145 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,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/beeper/nacserv-native/nac" | ||
"github.com/beeper/nacserv-native/requests" | ||
) | ||
|
||
var globalCert []byte | ||
|
||
func InitFetchCert(ctx context.Context) error { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, 30*time.Second) | ||
defer cancel() | ||
var err error | ||
globalCert, err = requests.FetchCert(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to fetch cert: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func InitSanityCheck() error { | ||
defer nac.MeowMemory()() | ||
return nac.SanityCheck() | ||
} | ||
|
||
const ValidityTime = 15 * time.Minute | ||
|
||
func GenerateValidationData(ctx context.Context) ([]byte, time.Time, error) { | ||
defer nac.MeowMemory()() | ||
|
||
validationCtx, request, err := nac.Init(globalCert) | ||
if err != nil { | ||
return nil, time.Time{}, err | ||
} | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, 30*time.Second) | ||
// Record valid until time before request, so it's definitely valid for at least that long | ||
validUntil := time.Now().UTC().Add(ValidityTime) | ||
sessionInfo, err := requests.InitializeValidation(ctx, request) | ||
cancel() | ||
if err != nil { | ||
return nil, validUntil, fmt.Errorf("failed to initialize validation: %w", err) | ||
} | ||
err = nac.KeyEstablishment(validationCtx, sessionInfo) | ||
if err != nil { | ||
return nil, validUntil, err | ||
} | ||
validationData, err := nac.Sign(validationCtx) | ||
if err != nil { | ||
return nil, validUntil, err | ||
} | ||
return validationData, validUntil, 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
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
Oops, something went wrong.