From 0ea2a50264c4eab43ac1a67cf4ddde62c9214a6d Mon Sep 17 00:00:00 2001 From: Qi Lin <141757486+QiL1n@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:36:13 +0800 Subject: [PATCH] Add option `realitySettings.masterKeyLog` --- infra/conf/transport_internet.go | 2 ++ transport/internet/reality/config.go | 17 ++++++++++++ transport/internet/reality/config.pb.go | 36 ++++++++++++++++--------- transport/internet/reality/config.proto | 1 + transport/internet/reality/reality.go | 1 + 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index 318eb336..8c113e92 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -420,6 +420,7 @@ func (c *TLSConfig) Build() (proto.Message, error) { type REALITYConfig struct { Show bool `json:"show"` + MasterKeyLog string `json:"masterKeyLog"` Dest json.RawMessage `json:"dest"` Type string `json:"type"` Xver uint64 `json:"xver"` @@ -440,6 +441,7 @@ type REALITYConfig struct { func (c *REALITYConfig) Build() (proto.Message, error) { config := new(reality.Config) config.Show = c.Show + config.MasterKeyLog = c.MasterKeyLog var err error if c.Dest != nil { var i uint16 diff --git a/transport/internet/reality/config.go b/transport/internet/reality/config.go index 58608720..be3611e5 100644 --- a/transport/internet/reality/config.go +++ b/transport/internet/reality/config.go @@ -1,7 +1,9 @@ package reality import ( + "io" "net" + "os" "time" "github.com/xtls/reality" @@ -25,6 +27,8 @@ func (c *Config) GetREALITYConfig() *reality.Config { NextProtos: nil, // should be nil SessionTicketsDisabled: true, + + KeyLogWriter: KeyLogWriterFromConfig(c), } config.ServerNames = make(map[string]bool) for _, serverName := range c.ServerNames { @@ -37,6 +41,19 @@ func (c *Config) GetREALITYConfig() *reality.Config { return config } +func KeyLogWriterFromConfig(c *Config) io.Writer { + if len(c.MasterKeyLog) <= 0 || c.MasterKeyLog == "none" { + return nil + } + + writer, err := os.OpenFile(c.MasterKeyLog, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644) + if err != nil { + newError("failed to open ", c.MasterKeyLog, " as master key log").AtError().Base(err).WriteToLog() + } + + return writer +} + func ConfigFromStreamSettings(settings *internet.MemoryStreamConfig) *Config { if settings == nil { return nil diff --git a/transport/internet/reality/config.pb.go b/transport/internet/reality/config.pb.go index 2b44d9b7..6631d0af 100644 --- a/transport/internet/reality/config.pb.go +++ b/transport/internet/reality/config.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.1 +// protoc-gen-go v1.32.0 +// protoc v4.25.1 // source: transport/internet/reality/config.proto package reality @@ -41,6 +41,7 @@ type Config struct { ShortId []byte `protobuf:"bytes,24,opt,name=short_id,json=shortId,proto3" json:"short_id,omitempty"` SpiderX string `protobuf:"bytes,25,opt,name=spider_x,json=spiderX,proto3" json:"spider_x,omitempty"` SpiderY []int64 `protobuf:"varint,26,rep,packed,name=spider_y,json=spiderY,proto3" json:"spider_y,omitempty"` + MasterKeyLog string `protobuf:"bytes,27,opt,name=master_key_log,json=masterKeyLog,proto3" json:"master_key_log,omitempty"` } func (x *Config) Reset() { @@ -187,6 +188,13 @@ func (x *Config) GetSpiderY() []int64 { return nil } +func (x *Config) GetMasterKeyLog() string { + if x != nil { + return x.MasterKeyLog + } + return "" +} + var File_transport_internet_reality_config_proto protoreflect.FileDescriptor var file_transport_internet_reality_config_proto_rawDesc = []byte{ @@ -194,7 +202,7 @@ var file_transport_internet_reality_config_proto_rawDesc = []byte{ 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1f, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, - 0x65, 0x74, 0x2e, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x22, 0xdc, 0x03, 0x0a, 0x06, 0x43, + 0x65, 0x74, 0x2e, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x22, 0x82, 0x04, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x68, 0x6f, 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x73, 0x68, 0x6f, 0x77, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, @@ -224,16 +232,18 @@ var file_transport_internet_reality_config_proto_rawDesc = []byte{ 0x74, 0x49, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x70, 0x69, 0x64, 0x65, 0x72, 0x5f, 0x78, 0x18, 0x19, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x70, 0x69, 0x64, 0x65, 0x72, 0x58, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x70, 0x69, 0x64, 0x65, 0x72, 0x5f, 0x79, 0x18, 0x1a, 0x20, 0x03, 0x28, 0x03, - 0x52, 0x07, 0x73, 0x70, 0x69, 0x64, 0x65, 0x72, 0x59, 0x42, 0x7f, 0x0a, 0x23, 0x63, 0x6f, 0x6d, - 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, - 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, - 0x50, 0x01, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, - 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x74, 0x72, - 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, - 0x2f, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0xaa, 0x02, 0x1f, 0x58, 0x72, 0x61, 0x79, 0x2e, - 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, - 0x65, 0x74, 0x2e, 0x52, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x52, 0x07, 0x73, 0x70, 0x69, 0x64, 0x65, 0x72, 0x59, 0x12, 0x24, 0x0a, 0x0e, 0x6d, 0x61, 0x73, + 0x74, 0x65, 0x72, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x6c, 0x6f, 0x67, 0x18, 0x1b, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0c, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x4b, 0x65, 0x79, 0x4c, 0x6f, 0x67, 0x42, + 0x7f, 0x0a, 0x23, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, + 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x72, + 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x50, 0x01, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, + 0x6f, 0x72, 0x65, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x72, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, 0xaa, 0x02, + 0x1f, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, + 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x52, 0x65, 0x61, 0x6c, 0x69, 0x74, 0x79, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/transport/internet/reality/config.proto b/transport/internet/reality/config.proto index f9ae3a4f..233f6e05 100644 --- a/transport/internet/reality/config.proto +++ b/transport/internet/reality/config.proto @@ -24,4 +24,5 @@ message Config { bytes short_id = 24; string spider_x = 25; repeated int64 spider_y = 26; + string master_key_log = 27; } diff --git a/transport/internet/reality/reality.go b/transport/internet/reality/reality.go index 75a668c9..136a075d 100644 --- a/transport/internet/reality/reality.go +++ b/transport/internet/reality/reality.go @@ -112,6 +112,7 @@ func UClient(c net.Conn, config *Config, ctx context.Context, dest net.Destinati ServerName: config.ServerName, InsecureSkipVerify: true, SessionTicketsDisabled: true, + KeyLogWriter: KeyLogWriterFromConfig(config), } if utlsConfig.ServerName == "" { utlsConfig.ServerName = dest.Address.String()