0

I'm writing a thrift service in Golang and I would like to understand how I can get the client's IP address in the handler functions implementation.

I have tried it in Java, which can be obtained by overwrite TProcessor#process(in,out), but it seems that this way doesn't work in Golang.

serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort(ip, strconv.FormatInt(port, 10)))
    //serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort("127.0.0.1", "9999"))
    if err != nil {
        log.Error(err.Error())
        os.Exit(1)
    }
    protocolFactory := thrift.NewTCompactProtocolFactory()
    transportFactory := thrift.NewTFramedTransportFactoryMaxLength(thrift.NewTTransportFactory(), 524288000) 

    processor := iface.NewNMPDataServiceProcessor(new(impl.NMPDataService))

    server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
    server.Serve()

    defer server.Stop()

The Handler implementation of NMPDataService interface:

package impl

import (
    "NMPService/Framework/logger"
    "NMPService/NmpService/thrift/iface"
    "NMPService/NmpService/utils"
    "syscall"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "path/filepath"
    "regexp"
    "strconv"
    "strings"
    "time"

    "github.com/yeka/zip"
)

var log = logger.GetBeeLogger()

// NMPDataService .
type NMPDataService struct {
}

const (
    // MaxDownloadFileLimit ...
    MaxDownloadFileLimit int64 = 15728640
    TempFolder string = "tmp"
)

// ExistsFolder method
// Parameters:
//  - Path
func (handler *NMPDataService) ExistsFolder(path string) (bool, error) {
    log.Info("Check exist folder, path: %s", path)
    if strings.Contains(path, "\\") {
        path = strings.ReplaceAll(path, "\\", "/")
    }
    fileInfo, err := os.Stat(path)
    if os.IsNotExist(err) {
        log.Error("folder %s not exist", path)
        return false, nil
    }

    if !fileInfo.IsDir() {
        msg := "the path is not folder."
        log.Error(msg)
        return false, NewRPCException(1, msg, errors.New(msg))
    }

    if err != nil {
        log.Error("Check exist folder error.")
        return false, NewRPCException(1, "Check exist folder error", err)
    }

    return true, nil
}

Above is my code on Server side. I want to get client IP information from server.

hnxydq
  • 1
  • 1
  • Where is the handler? – TehSphinX Sep 09 '19 at 18:42
  • Added some code of handler above. The handler is just the implementation of the interface. – hnxydq Sep 10 '19 at 01:34
  • I guess I can't help here. Usually I would expect some kind of connection in a handler to get the IP from. But I haven't worked with thrift and it seems to work differently. – TehSphinX Sep 10 '19 at 14:51
  • Possible duplicate of [How to get client's IP in a golang thrift server](https://stackoverflow.com/questions/44962798/how-to-get-clients-ip-in-a-golang-thrift-server) – JensG Sep 11 '19 at 23:01
  • BTW, Thrift 0.10.0 is not the most recent version. – JensG Sep 11 '19 at 23:02

0 Answers0