Skip to content

Commit 9c1b449

Browse files
committed
device: allow disabling configuration
1 parent 5d17e4b commit 9c1b449

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

cmd/device-client/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
)
2525

2626
type Config struct {
27-
Clients map[string]ClientConfig
27+
Clients map[string]ClientConfig
28+
CanFoaward bool
2829
}
2930

3031
type ClientConfig struct {
@@ -202,12 +203,15 @@ func createGoroutines(m *MachineData, dnsClient dns.Client, config Config) {
202203
for clientName, cc := range config.Clients {
203204
go func(clientName string, cc ClientConfig) {
204205
c, err := device.NewClient(&http.Client{
205-
Timeout: 5 * time.Second,
206-
//Transport: cc.transport,
206+
Timeout: 5 * time.Second,
207+
Transport: cc.transport,
207208
}, cc.BaseURL, cc.Token, cc.Network, cc.Device, cc.PrivateKey)
208209
if err != nil {
209210
panic(err)
210211
}
212+
if !config.CanFoaward {
213+
c.SetCanForward(false)
214+
}
211215
c.Machine = m.Machines[clientName]
212216
c.SetDNSClient(dnsClient)
213217
continuous := new(device.ContinousClient)

device/client.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math/rand"
1010
"net/http"
1111
"net/url"
12+
"slices"
1213
"sync"
1314

1415
"github.com/nyiyui/qrystal/coord"
@@ -29,6 +30,7 @@ type Client struct {
2930
goalHandle *goal.Handle
3031
dns dns.Client
3132
dnsLock sync.Mutex
33+
canForward bool
3234

3335
spec spec.SpecCensored
3436
token util.Token
@@ -65,6 +67,10 @@ func NewClient(httpClient *http.Client, baseURL string, token util.Token, networ
6567
}, nil
6668
}
6769

70+
func (c *Client) SetCanForward(canForward bool) {
71+
c.canForward = canForward
72+
}
73+
6874
func (c *Client) SetDNSClient(client dns.Client) {
6975
c.dnsLock.Lock()
7076
defer c.dnsLock.Unlock()
@@ -281,23 +287,25 @@ func (c *Client) patchAccessible(nc *spec.NetworkCensored) error {
281287
zap.S().Debugf("ndcI = %s", ndcI)
282288

283289
var accessible []string
284-
for _, ndc := range nc.Devices {
285-
if ndc.ForwarderAndEndpointChosen {
286-
accessible = append(accessible, ndc.Name)
290+
if c.canForward {
291+
for _, ndc := range nc.Devices {
292+
if ndc.ForwarderAndEndpointChosen {
293+
accessible = append(accessible, ndc.Name)
294+
}
287295
}
296+
zap.S().Debugf("I can forward for %d devices.", len(accessible))
297+
} else {
298+
zap.S().Debug("I cannot forward by configuration.")
288299
}
289-
if len(accessible) != 0 {
300+
if !slices.Equal(accessible, nc.Devices[ndcI].Accessible) {
290301
nc.Devices[ndcI].Accessible = accessible
291-
zap.S().Debugf("I can forward for %d devices.", len(accessible))
292302
err := c.patchSpec(coord.PatchReifySpecRequest{
293303
Accessible: accessible,
294304
AccessibleSet: true,
295305
})
296306
if err != nil {
297307
return fmt.Errorf("patch spec: %w", err)
298308
}
299-
} else {
300-
zap.S().Infof("I can't forward for any devices.")
301309
}
302310
return nil
303311
}

0 commit comments

Comments
 (0)