@@ -16,30 +16,92 @@ package fabrid
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"fmt"
20
21
"github.com/scionproto/scion/pkg/addr"
21
22
"github.com/scionproto/scion/pkg/daemon"
22
23
"github.com/scionproto/scion/pkg/private/serrors"
24
+ "io/ioutil"
25
+ "net/http"
23
26
)
24
27
25
28
// Result contains all the discovered paths.
26
29
type Result struct {
27
- Destination addr.IA `json:"destination" yaml:"destination"`
28
- Description string `json:"description,omitempty" yaml:"description,omitempty"`
30
+ Destination * addr.IA `json:"destination" yaml:"destination"`
31
+ Description string `json:"description,omitempty" yaml:"description,omitempty"`
29
32
}
30
33
31
34
// Run lists information for FABRID policies to stdout.
32
- func Run (ctx context.Context , dst addr.IA , cfg Config ) (* Result , error ) {
33
- sdConn , err := daemon .NewService (cfg .Daemon ).Connect (ctx )
35
+ func Run (ctx context.Context , dst * addr.IA , identifier uint32 , cfg Config ) (* Result , error ) {
36
+ var description string
37
+ if dst != nil {
38
+ sdConn , err := daemon .NewService (cfg .Daemon ).Connect (ctx )
39
+ if err != nil {
40
+ return nil , serrors .WrapStr ("connecting to the SCION Daemon" , err , "addr" , cfg .Daemon )
41
+ }
42
+ defer sdConn .Close ()
43
+
44
+ description , err = sdConn .RemotePolicyDescription (ctx , identifier , * dst )
45
+ if err != nil {
46
+ return nil , serrors .WrapStr ("retrieving description from the SCION Daemon" , err )
47
+ }
48
+ } else {
49
+ // Replace with the raw URL of your GitHub content (e.g., https://raw.githubusercontent.com/user/repo/branch/path/to/policies.json)
50
+ globalPolicyURL := "https://raw.githubusercontent.com/marcodermatt/fabrid-global-policies/main/policy-descriptions.json"
51
+
52
+ // Fetch the global policy from the URL
53
+ policy , err := FetchGlobalPolicy (globalPolicyURL )
54
+ if err != nil {
55
+ return nil , serrors .WrapStr ("fetching global policy" , err )
56
+ }
57
+
58
+ // Retrieve the description for the given identifier
59
+ description , err = GetPolicyDescription (policy , identifier )
60
+ if err != nil {
61
+ return nil , serrors .WrapStr ("getting global policy description" , err )
62
+ }
63
+
64
+ }
65
+ // Output the description
66
+ fmt .Printf ("Policy %d: %s\n " , identifier , description )
67
+ return & Result {Destination : dst , Description : description }, nil
68
+ }
69
+
70
+ // GlobalPolicy holds the mapping of uint32 identifiers to their string descriptions
71
+ type GlobalPolicy map [uint32 ]string
72
+
73
+ // FetchGlobalPolicy fetches and parses the global policy from the given URL
74
+ func FetchGlobalPolicy (url string ) (GlobalPolicy , error ) {
75
+ resp , err := http .Get (url )
34
76
if err != nil {
35
- return nil , serrors .WrapStr ("connecting to the SCION Daemon " , err , "addr" , cfg . Daemon )
77
+ return nil , serrors .WrapStr ("failed to fetch global policy " , err )
36
78
}
37
- defer sdConn .Close ()
79
+ defer resp . Body .Close ()
38
80
39
- description , err := sdConn .RemotePolicyDescription (ctx , 10 , dst )
81
+ if resp .StatusCode != http .StatusOK {
82
+ return nil , serrors .New ("failed to fetch global policy" , "StatusCode" , resp .StatusCode )
83
+ }
84
+
85
+ // Read the response body
86
+ body , err := ioutil .ReadAll (resp .Body )
40
87
if err != nil {
41
- return nil , serrors .WrapStr ("retrieving description from the SCION Daemon " , err )
88
+ return nil , serrors .WrapStr ("failed to read response body " , err )
42
89
}
43
- fmt .Println (description )
44
- return & Result {Destination : dst , Description : description }, nil
90
+
91
+ // Unmarshal the JSON data into a map
92
+ var policy GlobalPolicy
93
+ if err = json .Unmarshal (body , & policy ); err != nil {
94
+ return nil , serrors .WrapStr ("failed to unmarshal policy JSON" , err )
95
+ }
96
+
97
+ return policy , nil
98
+ }
99
+
100
+ // GetPolicyDescription retrieves the description for the given identifier
101
+ func GetPolicyDescription (policy GlobalPolicy , identifier uint32 ) (string , error ) {
102
+ description , exists := policy [identifier ]
103
+ if ! exists {
104
+ return "" , serrors .New ("no policy found" , "identifier" , identifier )
105
+ }
106
+ return description , nil
45
107
}
0 commit comments