Coder Social home page Coder Social logo

Comments (10)

ljusten avatar ljusten commented on June 18, 2024

Nice, thanks, ss is even faster than netstat. You can also set --forward-port=some available port to skip the port detection altogether. That's actually a bit faster as well.

I'd like to get rid of the whole netstat calling logic and detect the ports in the remote components (e.g. cdc_rsync_server) in code, but that'll require some bigger refactorings since right now cdc_rsync_server and port forwarding are started at the same time.

from cdc-file-transfer.

ljusten avatar ljusten commented on June 18, 2024

I'll replace the netstat command by

if which ss; then ss --numeric --listening --tcp; else netstat --numeric --listening --tcp; fi

from cdc-file-transfer.

tats-u avatar tats-u commented on June 18, 2024

@ljusten remote_util->BuildProcessStartInfoForSsh can handle shell scripts, right?

// static
absl::StatusOr<std::unordered_set<int>> PortManager::FindAvailableRemotePorts(
int first_port, int last_port, const char* ip,
ProcessFactory* process_factory, RemoteUtil* remote_util, int timeout_sec,
SteadyClock* steady_clock) {
// --numeric to get numerical addresses.
// --listening to get only listening sockets.
// --tcp to get only TCP connections.
std::string remote_command = "netstat --numeric --listening --tcp";
ProcessStartInfo start_info =
remote_util->BuildProcessStartInfoForSsh(remote_command);
start_info.name = "netstat";
start_info.flags = ProcessFlags::kNoWindow;
std::string output;
start_info.stdout_handler = [&output](const char* data, size_t data_size) {
output.append(data, data_size);
return absl::OkStatus();
};
std::string errors;
start_info.stderr_handler = [&errors](const char* data, size_t data_size) {
errors.append(data, data_size);
return absl::OkStatus();
};
std::unique_ptr<Process> process = process_factory->Create(start_info);
absl::Status status = process->Start();
if (!status.ok()) return WrapStatus(status, "Failed to start netstat");
Stopwatch timeout_timer(steady_clock);
bool is_timeout = false;
auto detect_timeout = [&timeout_timer, timeout_sec, &is_timeout]() {
is_timeout = timeout_timer.ElapsedSeconds() > timeout_sec;
return is_timeout;
};
status = process->RunUntil(detect_timeout);
if (!status.ok()) return WrapStatus(status, "Failed to run netstat process");
if (is_timeout)
return absl::DeadlineExceededError("Timeout while running netstat");
uint32_t exit_code = process->ExitCode();
if (exit_code != 0) {
return MakeStatus("netstat process exited with code %u:\n%s", exit_code,
errors);
}
LOG_DEBUG("netstat (instance) output:\n%s", output);
return FindAvailablePorts(first_port, last_port, output, ip);
}

The candidate you have just show me may not work if the remote shell is fish or other shells incompatible with sh.
It's great if we can grab the status code of a one-shot command (e.g. which ss) in a single function call.

from cdc-file-transfer.

ljusten avatar ljusten commented on June 18, 2024

That's true, but we already run similar code elsewhere, so at least it doesn't make the problem more severe. The real solution is to get rid of all shell scripts, but that's much more work.

absl::StrFormat("mkdir -p %s; if [ ! -f %s ]; then exit %i; fi; %s %i %s",

from cdc-file-transfer.

ljusten avatar ljusten commented on June 18, 2024

Actually, this should work in a wider range of (all?) shells:

which ss && ss --numeric --listening --tcp || netstat --numeric --listening --tcp

It also has the advantage that it falls back to netstat if executing ss fails.

from cdc-file-transfer.

tats-u avatar tats-u commented on June 18, 2024

@ljusten It has more portability. I'm afraid the ss's path vomited by which could occur unintended behavior.

$ which git && echo aaa || echo bbb
/usr/bin/git
aaa
$ which foo && echo aaa || echo bbb
bbb

Is it ignored when analyzed?

from cdc-file-transfer.

ljusten avatar ljusten commented on June 18, 2024

Yes, it's ignored when analyzed. It can be hidden by adding > /dev/null after which ss, but I think it's a useful thing to have in debug logs since this way we know which one was executed.

from cdc-file-transfer.

tats-u avatar tats-u commented on June 18, 2024

@ljusten I got it. I'll leave it to you. I've appreciated your quick response.

from cdc-file-transfer.

tats-u avatar tats-u commented on June 18, 2024

I found missing netstat is treated as the connection timeout without -vvv.
This causes users' misunderstanding.

from cdc-file-transfer.

tats-u avatar tats-u commented on June 18, 2024

@ljusten Thank you for the fix!

from cdc-file-transfer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.