Skip to content

Commit

Permalink
Fix timezone for Android and iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 21, 2024
1 parent cd0fcd5 commit 830ea46
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 7 deletions.
5 changes: 5 additions & 0 deletions constant/quic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build with_quic

package constant

const WithQUIC = true
5 changes: 5 additions & 0 deletions constant/quic_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !with_quic

package constant

const WithQUIC = false
1 change: 1 addition & 0 deletions experimental/libbox/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/sagernet/sing-box/common/humanize"
C "github.com/sagernet/sing-box/constant"
_ "github.com/sagernet/sing-box/include"
)

var (
Expand Down
5 changes: 2 additions & 3 deletions inbound/naive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/common/uot"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
Expand Down Expand Up @@ -109,8 +108,8 @@ func (n *Naive) Start() error {

if common.Contains(n.network, N.NetworkUDP) {
err := n.configureHTTP3Listener()
if !include.WithQUIC && len(n.network) > 1 {
log.Warn(E.Cause(err, "naive http3 disabled"))
if !C.WithQUIC && len(n.network) > 1 {
n.logger.Warn(E.Cause(err, "naive http3 disabled"))
} else if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions include/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ import (
_ "github.com/sagernet/sing-box/transport/v2rayquic"
_ "github.com/sagernet/sing-dns/quic"
)

const WithQUIC = true
2 changes: 0 additions & 2 deletions include/quic_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
N "github.com/sagernet/sing/common/network"
)

const WithQUIC = false

func init() {
dns.RegisterTransport([]string{"quic", "h3"}, func(name string, ctx context.Context, logger logger.ContextLogger, dialer N.Dialer, link string) (dns.Transport, error) {
return nil, C.ErrQUICNotIncluded
Expand Down
21 changes: 21 additions & 0 deletions include/tz_android.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89

package include

// #include <time.h>
import "C"
import "time"

func init() {
var currentT C.time_t
var currentTM C.struct_tm
C.time(&currentT)
C.localtime_r(&currentT, &currentTM)
tzOffset := int(currentTM.tm_gmtoff)
tz := C.GoString(currentTM.tm_zone)
time.Local = time.FixedZone(tz, tzOffset)
}
30 changes: 30 additions & 0 deletions include/tz_ios.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package include

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#import <Foundation/Foundation.h>
const char* getSystemTimeZone() {
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSString *timeZoneName = [timeZone description];
return [timeZoneName UTF8String];
}
*/
import "C"

import (
"strings"
"time"
)

func init() {
tzDescription := C.GoString(C.getSystemTimeZone())
if len(tzDescription) == 0 {
return
}
location, err := time.LoadLocation(strings.Split(tzDescription, " ")[0])
if err != nil {
return
}
time.Local = location
}

0 comments on commit 830ea46

Please sign in to comment.