HAProxy Lua how to change server port? - lua

Server.set_addr (sv, addr)
I can not understand what the method takes as input.
It would seem that Server.get_addr(sv) returns ip:port - so you also need to pass ip:port, but no. This does not work.
The documentation reads:
See the documentation for the control socket for more information on the string format.
Well, I saw that you need to transfer the ip port: port (example: 127.0.0.1 port 80). But that doesn't work either.
The only thing that works is pass only ip (example: 127.0.0.1).
The question is how to change the port?
Code sample: (if not work - no have any entry in log)
-- work
-- log entry: changed its IP from 1.1.1.1 to 2.2.2.2 by Lua script.
local newAdr = backendServer.ip
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. ":" .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " " .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " port " .. backendServer.port
server:set_addr(newAdr)

Update: As of HAProxy 2.2dev7, this functionality has now been merged: set_addr takes an additional optional parameter to specify a new port.
It's not currently possible to do that via Lua. A look through HAProxy's source code reveals that set_addr calls hlua_server_set_addr, which calls server_parse_addr_change_request, which calls update_server_addr, which only updates the address and not the port. Contrast this with the management socket's set server, which calls cli_parse_set_server, which calls update_server_addr_port, which does update the port too.
However, there are two pieces of good news:
Since the management socket can do it, you can use it as a workaround.
It would be very simple to modify HAProxy's Lua interface to support updating the port, since all the logic is already there. I sent a patch that does so to their mailing list.

Related

Is any way to start go_binary before java_test?

Our project has a few GRPC servers defined as go_binary targets. We develop client SDKs for Java and Python applications and we would like to use java_test and py_test. Is any way to start a specific go_binary target before java_test or py_test?
You can create a test harness that starts the gRPC server before running the tests. For example, you could add the binary to the data attribute of the test, and then started it beforehand:
go_binary(
name = "my_grpc_server",
[...]
)
py_test(
name = "my_test",
[...]
data = [":my_grpc_server"],
)
and then inside the test file:
class ClientTestCase(unittest.TestCase):
def setUp(self):
r = runfiles.Create()
self.server = subprocess.Popen([r.Rlocation("path/to/my_grpc_server")])
def tearDown(self):
self.server.terminate()
self.server.wait()
This example is very simple, you'll probably run into issues regarding the availability of the port the server listens on, or waiting for the server to start up. You could add flags to your gRPC server to allow communication over a domain socket, or make it listen on an unused port and have the test parse the port number from the server's log output.
For details on finding the server with runfiles: https://github.com/bazelbuild/bazel/blob/a7a0d48fbeb059ee60e77580e5d05baeefdd5699/tools/python/runfiles/runfiles.py#L16-L58
If you find yourself copy-pasting this pattern a lot, or having to implement it in multiple languages, you could try using an sh_test() rule to wrap the underlying py_test or java_test, and to start the server, then start the test with an environment variable telling it how to reach the server (eg MY_GRPC_SERVER_ADDRESS=localhost:${test_port}.

How to obtain bluetooth port direction with pyserial?

I'm trying to connect to an RN42, module through python. When the RN42 pairs with W10 it creates two virtual COM ports(outgoing and incoming). I need to connect to the outgoing port.
I'm trying to do this automatically. I've tried:
import serial
import serial.tools.list_ports as port_lst
ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
if 'Bluetooth' in p.description:
bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)
I thought that the first port was usually the outgoing one, but I've paired the module to another computer, and this didn't apply (the second port was the outgoing one). Is there a way to find out the direction of the COM ports?
Thanks!!!
Although this is an antique question, I have been searching for the answer to this for some time myself and since I finally figured it out I wanted others to be able to find the answer. With help from a blog entry at in the hand and its accompanying gist:
The trick is to acquire the hwid using pySerial, then parse the address. The incoming port in a pair has an address of zero and the outgoing port has a nonzero address. Here is some ugly Python code that decodes it:
import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
if "BTHENUM" in p.hwid:
start_of_address=p.hwid.rfind("&")
end_of_address=p.hwid.rfind("_")
address=p.hwid[start_of_address+1:end_of_address]
if int(address,16)==0:
port_type="incoming"
else:
port_type="outgoing"
print(p.hwid)
print(p.name, address, port_type)
And the output:
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing

Nmap match three ports

I'm trying to make a script for nmap, which only executes its action, if three ports are open.
The end result would be if port 515, 631 and 9100 are open for tcp traffic, script should print a message (at first, later it should make a os detection).
My script so far:
function portrule(host,port)
return port.state == 'open' and
port.protocol == 'tcp' and
515 <= port.number and
631 <= port.number and
9100 <= port.number
end
action = function(host, port)
return "hello"
end
But this will execute for all ports after 9100 is hit.
Any help is appriciated!
NSE scripts match based on a rule function, and there are four different types. You are using a portrule, which tests each port on a host. Since you want to test multiple ports on a host, you should be using a hostrule instead.
The hostrule is tested after the state of all ports is known. You can check the state of all of these ports by using the nmap.get_ports function like nfs-ls does, or you can check individual ports using the nmap.get_port_state function like the nbstat script does. Actually, nbstat looks like a good example of exactly what you want to do.

Telnet server example

Today I installed NodeMCU on one ESP8266 module.
I made a simple telnet server (logging in to the AP is already done and connected)
srv=net.createServer(net.TCP,7200)
srv:listen(23,function(conn)
conn:on("receive", function(conn,telnetdata)
print(telnetdata)
conn:send("Got it\r\n")
end)
conn:on("sent",function(conn)
print("[Sent]");
--conn:close();
--collectgarbage();
end)
end)
I can telnet into the ESP8266 using Putty and I can send static data back to Putty (conn:send("Got it\r\n")).
What I want to do is to send data to Putty from the esp serial port (UART). I've googled for an example without success. BTW I'm using ESPlorer to program the ESP and to send data back to putty.
Instead of using conn:send("Got it\r\n") I want to dynamically type the response. I've tried uart:on callback but I haven't fully understood how it works.
NodeMCU has an "official" Telnet example in the repository. However, that one is also WiFi/socket-based.
If you're connected to the device through ESPlorer (i.e. serial/UART) you can't open another serial connection. See https://stackoverflow.com/a/36779799/131929 for details.
Marcel thank you for your answer.
The link posted is not exactly what I need but it offered good info.
I finally came up with this code that works as a simple bridge between putty and a micro controller (arduino mega 2560 in this case)
socket = net.createServer(net.TCP,7200)
socket:listen(23,function(c)
c:on("receive",function(c,l)
uart.write(0, l)
end)
c:on("disconnection",function(c)
-- not yet implemented
end)
uart.on("data", 0,
function(data)
c:send(data)
end, 0)
end)

Print to remote Comm Port in VB6

I have a software that prints out to a thermal printer connected to the comm port, the code below works well.
Open "COM3:" For Output Access Write As #1
Print #1, Space(8) & "FRAKAS LTD"
Print #1, Space(7) & "P.O. Box 7678 SHIMONI"
...
...
Close #1
Now I want to print on the same comm port but from a different computer on the network, is it possible?
You can redirect the serial port across the network by running software at each end. There is some info on Wikipedia here about some solutions - I haven't tried any.
We have used ethernet to serial converters from Moxa - these redirect a local COM port to a remote one without any PC at the remote end. One example is here
if the printer is availabe then you can use the following to prepare text to be printed :
printer.print "text"
to start the actual printing you use the following :
printer.enddoc

Resources