Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package common |
| 21 | |
| 22 | import ( |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 23 | "compress/zlib" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 24 | "crypto/tls" |
| 25 | "flag" |
| 26 | "fmt" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 27 | |
| 28 | "github.com/apache/thrift/lib/go/thrift" |
| 29 | "github.com/apache/thrift/test/go/src/gen/thrifttest" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | debugServerProtocol bool |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | func init() { |
| 37 | flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on") |
| 38 | } |
| 39 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 40 | func GetServerParams( |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 41 | host string, |
| 42 | port int64, |
| 43 | domain_socket string, |
| 44 | transport string, |
| 45 | protocol string, |
| 46 | ssl bool, |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 47 | certPath string, |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 48 | handler thrifttest.ThriftTest, |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 49 | ) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, string /* addr */, error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 50 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 51 | var err error |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 52 | hostPort := fmt.Sprintf("%s:0", host) |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 53 | var cfg *thrift.TConfiguration = nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 54 | |
| 55 | var protocolFactory thrift.TProtocolFactory |
| 56 | switch protocol { |
| 57 | case "compact": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 58 | protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 59 | case "simplejson": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 60 | protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 61 | case "json": |
| 62 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 63 | case "binary": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 64 | protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil) |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 65 | case "header": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 66 | protocolFactory = thrift.NewTHeaderProtocolFactoryConf(nil) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 67 | default: |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 68 | return nil, nil, nil, nil, "", fmt.Errorf("invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 69 | } |
| 70 | if debugServerProtocol { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 71 | protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "server:", thrift.StdLogger(nil)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | var serverTransport thrift.TServerTransport |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 75 | var addr string |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 76 | if ssl { |
| 77 | cfg := new(tls.Config) |
| 78 | if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 79 | return nil, nil, nil, nil, "", err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 80 | } else { |
| 81 | cfg.Certificates = append(cfg.Certificates, cert) |
| 82 | } |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 83 | transport, transportErr := thrift.NewTSSLServerSocket(hostPort, cfg) |
| 84 | if transportErr == nil { |
| 85 | listenErr := transport.Listen() |
| 86 | if listenErr == nil { |
| 87 | serverTransport = transport |
| 88 | addr = transport.Addr().String() |
| 89 | } else { |
| 90 | err = listenErr |
| 91 | } |
| 92 | } else { |
| 93 | err = transportErr |
| 94 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 95 | } else { |
| 96 | if domain_socket != "" { |
| 97 | serverTransport, err = thrift.NewTServerSocket(domain_socket) |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 98 | addr = domain_socket |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 99 | } else { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 100 | transport, transportErr := thrift.NewTServerSocket(hostPort) |
| 101 | if transportErr == nil { |
| 102 | listenErr := transport.Listen() |
| 103 | if listenErr == nil { |
| 104 | serverTransport = transport |
| 105 | addr = transport.Addr().String() |
| 106 | } else { |
| 107 | err = listenErr |
| 108 | } |
| 109 | } else { |
| 110 | err = transportErr |
| 111 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | if err != nil { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 115 | return nil, nil, nil, nil, "", err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | var transportFactory thrift.TTransportFactory |
| 119 | |
| 120 | switch transport { |
| 121 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 122 | // there is no such factory, and we don't need any |
| 123 | transportFactory = nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 124 | case "framed": |
| 125 | transportFactory = thrift.NewTTransportFactory() |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 126 | transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, nil) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 127 | case "buffered": |
| 128 | transportFactory = thrift.NewTBufferedTransportFactory(8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 129 | case "zlib": |
| 130 | transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 131 | case "": |
| 132 | transportFactory = thrift.NewTTransportFactory() |
| 133 | default: |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 134 | return nil, nil, nil, nil, "", fmt.Errorf("invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 135 | } |
| 136 | processor := thrifttest.NewThriftTestProcessor(handler) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 137 | |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame^] | 138 | return processor, serverTransport, transportFactory, protocolFactory, addr, nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 139 | } |