-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Incoming Properties #88
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bac0abd
R: rename runtimecontext to cfcontext
aki-0421 cdd8a26
F: add incoming property context
aki-0421 bb2644d
F: add incoming property pkg
aki-0421 e5294f4
F: add incoming property example
aki-0421 46459b4
R: fix
aki-0421 b0d246c
T: fix test error
aki-0421 7f9e061
F: complete implementation
aki-0421 4035675
R: remove json tag
aki-0421 2eb4020
R: refactor context
aki-0421 a067e8e
R: refactor cron with new runtimecontext
aki-0421 83be8bc
R: refactor incoming with new runtimecontext
aki-0421 86f9f1c
R: fix illegal invocation error
aki-0421 eac1510
R: merge pkg
aki-0421 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,3 @@ | ||
build | ||
node_modules | ||
.wrangler |
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,12 @@ | ||
.PHONY: dev | ||
dev: | ||
wrangler dev | ||
|
||
.PHONY: build | ||
build: | ||
go run ../../cmd/workers-assets-gen | ||
tinygo build -o ./build/app.wasm -target wasm -no-debug ./... | ||
|
||
.PHONY: deploy | ||
deploy: | ||
wrangler deploy |
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,7 @@ | ||
module github.com/syumai/workers/_examples/hello | ||
|
||
go 1.21.3 | ||
|
||
require github.com/syumai/workers v0.0.0 | ||
|
||
replace github.com/syumai/workers => ../../ |
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,2 @@ | ||
github.com/syumai/workers v0.1.0 h1:z5QfQR2X+PCKzom7RodpI5J4D5YF7NT7Qwzb9AM9dgY= | ||
github.com/syumai/workers v0.1.0/go.mod h1:alXIDhTyeTwSzh0ZgQ3cb9HQPyyYfIejupE4Z3efr14= |
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,24 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/syumai/workers" | ||
"github.com/syumai/workers/cloudflare/incoming" | ||
) | ||
|
||
func main() { | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
p := incoming.NewProperties(req.Context()) | ||
|
||
encoder := json.NewEncoder(w) | ||
w.Header().Set("Content-Type", "application/json") | ||
if err := encoder.Encode(p); err != nil { | ||
http.Error(w, fmt.Sprintf("Error encoding JSON: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
workers.Serve(handler) | ||
} |
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,9 @@ | ||
name = "incoming" | ||
main = "./build/worker.mjs" | ||
compatibility_date = "2022-05-13" | ||
compatibility_flags = [ | ||
"streams_enable_constructors" | ||
] | ||
|
||
[build] | ||
command = "make build" |
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,159 @@ | ||
package incoming | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
import ( | ||
"context" | ||
"syscall/js" | ||
|
||
"github.com/syumai/workers/internal/cfcontext" | ||
"github.com/syumai/workers/internal/jsutil" | ||
) | ||
|
||
type BotManagementJsDetection struct { | ||
Passed bool | ||
} | ||
|
||
func NewBotManagementJsDetection(obj js.Value) *BotManagementJsDetection { | ||
if obj.IsUndefined() { | ||
return nil | ||
} | ||
return &BotManagementJsDetection{ | ||
Passed: obj.Get("passed").Bool(), | ||
} | ||
} | ||
|
||
type BotManagement struct { | ||
CorporateProxy bool | ||
VerifiedBot bool | ||
JsDetection *BotManagementJsDetection | ||
StaticResource bool | ||
Score int | ||
} | ||
|
||
func NewBotManagement(obj js.Value) *BotManagement { | ||
if obj.IsUndefined() { | ||
return nil | ||
} | ||
return &BotManagement{ | ||
CorporateProxy: obj.Get("corporateProxy").Bool(), | ||
VerifiedBot: obj.Get("verifiedBot").Bool(), | ||
JsDetection: NewBotManagementJsDetection(obj.Get("jsDetection")), | ||
StaticResource: obj.Get("staticResource").Bool(), | ||
Score: obj.Get("score").Int(), | ||
} | ||
} | ||
|
||
type TLSClientAuth struct { | ||
CertIssuerDNLegacy string | ||
CertIssuerSKI string | ||
CertSubjectDNRFC2253 string | ||
CertSubjectDNLegacy string | ||
CertFingerprintSHA256 string | ||
CertNotBefore string | ||
CertSKI string | ||
CertSerial string | ||
CertIssuerDN string | ||
CertVerified string | ||
CertNotAfter string | ||
CertSubjectDN string | ||
CertPresented string | ||
CertRevoked string | ||
CertIssuerSerial string | ||
CertIssuerDNRFC2253 string | ||
CertFingerprintSHA1 string | ||
} | ||
|
||
func NewTLSClientAuth(obj js.Value) *TLSClientAuth { | ||
if obj.IsUndefined() { | ||
return nil | ||
} | ||
return &TLSClientAuth{ | ||
CertIssuerDNLegacy: jsutil.MaybeString(obj.Get("certIssuerDNLegacy")), | ||
CertIssuerSKI: jsutil.MaybeString(obj.Get("certIssuerSKI")), | ||
CertSubjectDNRFC2253: jsutil.MaybeString(obj.Get("certSubjectDNRFC2253")), | ||
CertSubjectDNLegacy: jsutil.MaybeString(obj.Get("certSubjectDNLegacy")), | ||
CertFingerprintSHA256: jsutil.MaybeString(obj.Get("certFingerprintSHA256")), | ||
CertNotBefore: jsutil.MaybeString(obj.Get("certNotBefore")), | ||
CertSKI: jsutil.MaybeString(obj.Get("certSKI")), | ||
CertSerial: jsutil.MaybeString(obj.Get("certSerial")), | ||
CertIssuerDN: jsutil.MaybeString(obj.Get("certIssuerDN")), | ||
CertVerified: jsutil.MaybeString(obj.Get("certVerified")), | ||
CertNotAfter: jsutil.MaybeString(obj.Get("certNotAfter")), | ||
CertSubjectDN: jsutil.MaybeString(obj.Get("certSubjectDN")), | ||
CertPresented: jsutil.MaybeString(obj.Get("certPresented")), | ||
CertRevoked: jsutil.MaybeString(obj.Get("certRevoked")), | ||
CertIssuerSerial: jsutil.MaybeString(obj.Get("certIssuerSerial")), | ||
CertIssuerDNRFC2253: jsutil.MaybeString(obj.Get("certIssuerDNRFC2253")), | ||
CertFingerprintSHA1: jsutil.MaybeString(obj.Get("certFingerprintSHA1")), | ||
} | ||
} | ||
|
||
type TLSExportedAuthenticator struct { | ||
ClientFinished string | ||
ClientHandshake string | ||
ServerHandshake string | ||
ServerFinished string | ||
} | ||
|
||
func NewTLSExportedAuthenticator(obj js.Value) *TLSExportedAuthenticator { | ||
if obj.IsUndefined() { | ||
return nil | ||
} | ||
return &TLSExportedAuthenticator{ | ||
ClientFinished: jsutil.MaybeString(obj.Get("clientFinished")), | ||
ClientHandshake: jsutil.MaybeString(obj.Get("clientHandshake")), | ||
ServerHandshake: jsutil.MaybeString(obj.Get("serverHandshake")), | ||
ServerFinished: jsutil.MaybeString(obj.Get("serverFinished")), | ||
} | ||
} | ||
|
||
type Properties struct { | ||
Longitude string | ||
Latitude string | ||
TlsCipher string | ||
Continent string | ||
Asn int | ||
ClientAcceptEncoding string | ||
Country string | ||
TLSClientAuth *TLSClientAuth | ||
TLSExportedAuthenticator *TLSExportedAuthenticator | ||
TlsVersion string | ||
Colo string | ||
Timezone string | ||
City string | ||
VerifiedBotCategory string | ||
// EdgeRequestKeepAliveStatus int | ||
RequestPriority string | ||
HttpProtocol string | ||
Region string | ||
RegionCode string | ||
AsOrganization string | ||
PostalCode string | ||
BotManagement *BotManagement | ||
} | ||
|
||
func NewProperties(ctx context.Context) *Properties { | ||
obj := cfcontext.MustExtractIncomingProperty(ctx) | ||
return &Properties{ | ||
Longitude: jsutil.MaybeString(obj.Get("longitude")), | ||
Latitude: jsutil.MaybeString(obj.Get("latitude")), | ||
TlsCipher: jsutil.MaybeString(obj.Get("tlsCipher")), | ||
Continent: jsutil.MaybeString(obj.Get("continent")), | ||
Asn: obj.Get("asn").Int(), | ||
ClientAcceptEncoding: jsutil.MaybeString(obj.Get("clientAcceptEncoding")), | ||
Country: jsutil.MaybeString(obj.Get("country")), | ||
TLSClientAuth: NewTLSClientAuth(obj.Get("tlsClientAuth")), | ||
TLSExportedAuthenticator: NewTLSExportedAuthenticator(obj.Get("tlsExportedAuthenticator")), | ||
TlsVersion: obj.Get("tlsVersion").String(), | ||
Colo: obj.Get("colo").String(), | ||
Timezone: obj.Get("timezone").String(), | ||
City: jsutil.MaybeString(obj.Get("city")), | ||
VerifiedBotCategory: jsutil.MaybeString(obj.Get("verifiedBotCategory")), | ||
RequestPriority: jsutil.MaybeString(obj.Get("requestPriority")), | ||
HttpProtocol: obj.Get("httpProtocol").String(), | ||
Region: jsutil.MaybeString(obj.Get("region")), | ||
RegionCode: jsutil.MaybeString(obj.Get("regionCode")), | ||
AsOrganization: obj.Get("asOrganization").String(), | ||
PostalCode: jsutil.MaybeString(obj.Get("postalCode")), | ||
BotManagement: NewBotManagement(obj.Get("botManagement")), | ||
} | ||
} |
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,39 @@ | ||
package cfcontext | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"syscall/js" | ||
) | ||
|
||
type runtimeCtxKey struct{} | ||
type incomingPropertyKey struct{} | ||
|
||
func New(ctx context.Context, runtimeCtxObj, incomingPropertyObj js.Value) context.Context { | ||
aki-0421 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx = context.WithValue(ctx, runtimeCtxKey{}, runtimeCtxObj) | ||
ctx = context.WithValue(ctx, incomingPropertyKey{}, incomingPropertyObj) | ||
return ctx | ||
} | ||
|
||
var ErrRuntimeContextNotFound = errors.New("runtime context was not found") | ||
var ErrIncomingPropertyNotFound = errors.New("incoming property was not found") | ||
|
||
// MustExtractRuntimeContext extracts runtime context object from context. | ||
// This function panics when runtime context object was not found. | ||
func MustExtractRuntimeContext(ctx context.Context) js.Value { | ||
v, ok := ctx.Value(runtimeCtxKey{}).(js.Value) | ||
if !ok { | ||
panic(ErrRuntimeContextNotFound) | ||
} | ||
return v | ||
} | ||
|
||
// MustExtractIncomingProperty extracts incoming property object from context. | ||
// This function panics when incoming property object was not found. | ||
func MustExtractIncomingProperty(ctx context.Context) js.Value { | ||
v, ok := ctx.Value(incomingPropertyKey{}).(js.Value) | ||
if !ok { | ||
panic(ErrIncomingPropertyNotFound) | ||
} | ||
return v | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.