dgofman / nats_client

MIT License
16 stars 1 forks source link

Could not connect to nats server with tls enabled #1

Closed httuan250591 closed 2 years ago

httuan250591 commented 2 years ago

Client code:

 final conn = await Nats.connect(
          opts: {'servers': 'wss://server:4222', 'tls': true},
          authenticator: UserAuthenticator.create('user', 'pass'),
          statusCallback: (status, error) {
            dev.log('$runtimeType, $status (${error.toString()})');
          });
      conn.subscribe('chat', (Result result) {
        dev.log('$runtimeType, ${result.data}');
      });

Error log:

NatConnection, Status.STALE_CONNECTION (WebSocketChannelException: WebSocketChannelException: HandshakeException: Handshake error in client (OS Error: 
WRONG_VERSION_NUMBER(tls_record.cc:242)))
dgofman commented 2 years ago

Probably your server is not configured for WebSocket connection Please update your config file

websocket { port: 8080 no_tls: true }

nileshsoni97 commented 2 years ago

yes there is an issue while we enable ws on server with token auth when i add in token in opts ex. //. 'token': 'dfvndofvndvdnvodnveo', this give me error of Status.ERROR ('Authorization Violation') but same code working fine on node js client with token auth.

Kindly fix it asap.

dgofman commented 2 years ago

@nileshsoni97 Did you follow my server installation in the README file?

https://github.com/dgofman/nats_client/blob/master/README.md

nileshsoni97 commented 2 years ago

Yes here is my code

  ` final conn = await Nats.connect(
          opts: {
            'servers': 'ws://111.111.111.111:111',
            'reconnectTimeWait': 2000, // 2s
            'pingInterval': 10000, //10s
            'reconnect': true,
            'maxReconnectAttempts': 0,
            //'token':'token',
            // 'user': "rs",
            // 'pass': "dmsodknoeirnfe "
          },
          debug: false,
          authenticator: JwtAuthenticator.create("token"),
          statusCallback: (status, error) async {
            if (error != null) {
              print('natsClient : ERROR $error');
            }
            if (status == Status.PING_TIMER) {
              print("natsClient : ${status.toString()}");
            } else if (status == Status.CONNECT) {
              print("natsClient : ${status.toString()}");
            }
          });
      conn.subscribe('hello', (Result result) {
        print("natsClient : ${utf8.decode(result.data)}");
      });`

but still getting same error Status.ERROR ('Authorization Violation')

nileshsoni97 commented 2 years ago

Server code :

   `port: 111 # Listening port for client connection
    server_name : "nats-default"
    http: localhost:110 # For Monitering

#tls: {
 # cert_file: "C:/certificate/cert.cer"
 # key_file: "C:/certificate/cert.key"
 # ca_file: "C:/certificate/cert.pem"
 # insecure : true
#}

# Log options
debug: false
trace: false
logtime: true
log_file: "C:/tmp/gnatsd.log"

authorization: {
  token:token
  timeout: 3
   # users: [
    #    {user: r, password : dmsodknoeirnfe},
     #   {user: w, password : sndijnviuneiunre}]
}

websocket {
# host: "0.0.0.0"
port: 11112
no_tls: true
authorization {
token:token
timeout: 3
  #  users: [
   #     {user: r, password : dmsodknoeirnfe},
    #    {user: w, password : sndijnviuneiunre}
#]
}
allowed_origins [
     "*"
   ]
}

jetstream {
   store_dir=nats
   // 1GB
   max_memory_store: 1073741824
   // 1GB
   max_file_store: 1073741824
}`

But now i moved to user, pass auth and it's best with permission so now no needs of token auth. Thanks for your contribution to Nats i appreciate your work.

dgofman commented 2 years ago

What kind of IP address are you using '111.111.111.111:111' (5 pairs)

nileshsoni97 commented 2 years ago

This is random i just replaced original values.

nileshsoni97 commented 2 years ago

last one is port :111 only 4 pairs

dgofman commented 2 years ago

JwtAuthenticator.create("token"), == CREATE USER TOKEN - nsc add user --bearer --name

"--bearer " must be set

nileshsoni97 commented 2 years ago

Thanks for your help but now i used user-pass auth and that is awesome one again i appreciate you.