@@ -72,9 +72,32 @@ func newWush(cfg js.Value) map[string]any {
72
72
cfg .Get ("onWebrtcCandidate" ),
73
73
)
74
74
75
- err = ov .PickDERPHome (ctx )
76
- if err != nil {
77
- panic (err )
75
+ // Try to get stored DERP home from localStorage
76
+ localStorage := js .Global ().Get ("localStorage" )
77
+ storedDerpHome := localStorage .Call ("getItem" , "derpHome" )
78
+ if ! storedDerpHome .IsNull () {
79
+ // Parse stored DERP home and use it
80
+ derpID := uint16 (js .Global ().Get ("parseInt" ).Invoke (storedDerpHome ).Int ())
81
+ if region := dm .Regions [int (derpID )]; region != nil {
82
+ ov .DerpRegionID = derpID
83
+ hlog ("Using stored DERP home: %s" , region .RegionName )
84
+ } else {
85
+ // If stored DERP home is invalid, pick a new one
86
+ err = ov .PickDERPHome (ctx )
87
+ if err != nil {
88
+ panic (err )
89
+ }
90
+ // Store the newly picked DERP home
91
+ localStorage .Call ("setItem" , "derpHome" , fmt .Sprint (ov .DerpRegionID ))
92
+ }
93
+ } else {
94
+ // No stored DERP home, pick a new one
95
+ err = ov .PickDERPHome (ctx )
96
+ if err != nil {
97
+ panic (err )
98
+ }
99
+ // Store the picked DERP home
100
+ localStorage .Call ("setItem" , "derpHome" , fmt .Sprint (ov .DerpRegionID ))
78
101
}
79
102
80
103
s , err := tsserver .NewServer (ctx , logger , ov , dm )
@@ -91,11 +114,13 @@ func newWush(cfg js.Value) map[string]any {
91
114
panic (err )
92
115
}
93
116
94
- _ , err = ts .Up (ctx )
95
- if err != nil {
96
- panic (err )
97
- }
98
- hlog ("WireGuard is ready" )
117
+ go func () {
118
+ _ , err = ts .Up (ctx )
119
+ if err != nil {
120
+ panic (err )
121
+ }
122
+ hlog ("WireGuard is ready" )
123
+ }()
99
124
100
125
cpListener , err := ts .Listen ("tcp" , ":4444" )
101
126
if err != nil {
0 commit comments