Files
openchamber-android-native/third_party/mygo/internal/windows/network.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

98 lines
3.5 KiB
Go

//go:build windows && (amd64 || arm64)
package windows
import (
"syscall"
"unsafe"
"github.com/egoist/mygo/internal/platform"
)
var iphlpapi = systemDLL("iphlpapi.dll")
// Both are on Windows 10 2004 and later.
var (
procGetNetworkConnectivityHint = iphlpapi.NewProc("GetNetworkConnectivityHint")
procNotifyNetworkConnectivityHintChange = iphlpapi.NewProc("NotifyNetworkConnectivityHintChange")
)
// NL_NETWORK_CONNECTIVITY_LEVEL_HINT and NL_NETWORK_CONNECTIVITY_COST_HINT.
const (
nlConnectivityLevelUnknown = 0
nlConnectivityLevelNone = 1
nlConnectivityLevelHidden = 5
nlConnectivityCostFixed = 2
nlConnectivityCostVariable = 3
)
// nlNetworkConnectivityHint mirrors NL_NETWORK_CONNECTIVITY_HINT of nldef.h:
// the connectivity level and cost are C enums (int, 4 bytes each), followed
// by three BOOLEANs.
type nlNetworkConnectivityHint struct {
Level int32
Cost int32
ApproachingLimit uint8
OverLimit uint8
Roaming uint8
}
func (b *Backend) Network() platform.Network { return network{b} }
type network struct{ b *Backend }
// Status reads the system's connectivity hint, the aggregate of what the
// Network Connectivity Status Indicator found out on every interface.
func (network) Status() platform.NetworkStatus {
var h nlNetworkConnectivityHint
if !has(procGetNetworkConnectivityHint) {
// An older Windows does not tell. Assume the connection works and
// is not metered, as the browsers do.
return platform.NetworkStatus{Online: true}
}
if r, _, _ := procGetNetworkConnectivityHint.Call(uintptr(unsafe.Pointer(&h))); r != 0 {
return platform.NetworkStatus{Online: true}
}
return networkStatus(h)
}
// networkStatus reads a connectivity hint as macOS and Linux report
// theirs. Online is a connection beyond the computer, as their route out:
// local access (a network, which the system found no way to the internet
// from), internet access and constrained internet access (behind a
// captive portal) count, no connectivity and a hidden network, whose
// routes the system ignores, do not; an unknown level is taken to work,
// as on an older Windows. Metered follows Microsoft's guidance for
// metered connections (Windows.Networking.Connectivity's ConnectionCost):
// any cost but unrestricted, or roaming, over the data limit or
// approaching it, asks to hold back.
func networkStatus(h nlNetworkConnectivityHint) platform.NetworkStatus {
return platform.NetworkStatus{
Online: h.Level != nlConnectivityLevelNone && h.Level != nlConnectivityLevelHidden,
Metered: h.Cost == nlConnectivityCostFixed || h.Cost == nlConnectivityCostVariable ||
h.Roaming != 0 || h.OverLimit != 0 || h.ApproachingLimit != 0,
}
}
// networkHandle is the registration of the hint's changes, which lives
// as long as the app.
var networkHandle uintptr
// Watch asks to be told when the connectivity hint changes. The core calls
// it once, after the application window exists.
func (n network) Watch() {
if !has(procNotifyNetworkConnectivityHintChange) {
return
}
// The callback runs on a thread of the system's, so it only posts to
// the application window, and the main thread reads the hint again.
// Its second parameter is the hint by value, which amd64 passes as a
// pointer and arm64 in two registers, so it is not read.
cb := syscall.NewCallback(func(ctx, hint uintptr) uintptr {
postMessage(n.b.appHwnd, wmAppNetwork, 0, 0)
return 0
})
procNotifyNetworkConnectivityHintChange.Call(cb, 0, 0, uintptr(unsafe.Pointer(&networkHandle)))
}