Skip to content

Commit

Permalink
optimize: error message show details, change scrcpy-server to /tmp to…
Browse files Browse the repository at this point in the history
… prevent permission issues

fix issue: serial not save defaults
  • Loading branch information
wsvn53 committed Jul 25, 2021
1 parent dd84a7f commit 823e2b1
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 57 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ scrcpy-init: libsdl ffmpeg libssh
# fix build issues
# -> remove windows platform code
rm -v scrcpy-src/app/src/sys/win/process.c
# -> change PREFIX to /tmp
grep "undef PREFIX" ./scrcpy-src/x/app/config.h || echo "#undef PREFIX" >> ./scrcpy-src/x/app/config.h
grep "/tmp" ./scrcpy-src/x/app/config.h || echo "#define PREFIX \"/tmp\"" >> ./scrcpy-src/x/app/config.h

.PHONY: scrcpy-server
8 changes: 4 additions & 4 deletions Scrcpy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2021072402;
CURRENT_PROJECT_VERSION = 2021072501;
DEVELOPMENT_TEAM = K8Q74NT2BG;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = "$(SRCROOT)/ssh";
Expand All @@ -709,7 +709,7 @@
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(SRCROOT)/Libs/lib";
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.1;
OTHER_LDFLAGS = (
"-lSDL2",
"-lavformat",
Expand Down Expand Up @@ -749,7 +749,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2021072402;
CURRENT_PROJECT_VERSION = 2021072501;
DEVELOPMENT_TEAM = K8Q74NT2BG;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = "$(SRCROOT)/ssh";
Expand All @@ -775,7 +775,7 @@
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(SRCROOT)/Libs/lib";
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.1;
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = (
"-lSDL2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<key>scrcpy-fix.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>scrcpy-ios.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
Expand Down
2 changes: 1 addition & 1 deletion Scrcpy/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Optional" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="mBv-x2-juS">
<rect key="frame" x="162.5" y="5" width="191.5" height="33.5"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" returnKeyType="done"/>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Device Serial:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lZe-wP-s8I">
<rect key="frame" x="20" y="11" width="103.5" height="21.5"/>
Expand Down
2 changes: 0 additions & 2 deletions Scrcpy/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<dict>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
</dict>
Expand Down
25 changes: 6 additions & 19 deletions Scrcpy/ScrcpyViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ - (void)setupViews {
self.sshPort.delegate = (id<UITextFieldDelegate>)self;
self.sshUser.delegate = (id<UITextFieldDelegate>)self;
self.sshPassword.delegate = (id<UITextFieldDelegate>)self;
self.adbSerial.delegate = (id<UITextFieldDelegate>)self;
}

- (void)loadForm {
Expand All @@ -75,6 +76,7 @@ - (void)loadForm {
self.sshPort.text = [SSHParams sharedParams].sshPort;
self.sshUser.text = [SSHParams sharedParams].sshUser;
self.sshPassword.text = [SSHParams sharedParams].sshPassword;
self.adbSerial.text = [SSHParams sharedParams].adbSerial;
}

#pragma mark - Actions
Expand All @@ -91,32 +93,17 @@ - (IBAction)startScrcpy:(id)sender {
NSString *port = self.sshPort.text;
NSString *user = self.sshUser.text;
NSString *password = self.sshPassword.text;
NSString *serial = self.adbSerial.text;

// Check & Save SSH connnection parameters
CheckParam(server, @"ssh server");
CheckParam(port, @"ssh port");
CheckParam(user, @"ssh user");
CheckParam(password, @"password");
[SSHParams setParamsWithServer:server port:port user:user password:password];
[SSHParams setParamsWithServer:server port:port user:user password:password serial:serial];

// Start scrcpy by detach from current stack
[self performSelector:@selector(scrcpyMain) withObject:nil afterDelay:0];
}

- (void)autoStartScrcpy {
// Check SSH parameters
NSString *server = self.sshServer.text;
NSString *port = self.sshPort.text;
NSString *user = self.sshUser.text;
NSString *password = self.sshPassword.text;

if (server.length == 0 || port.length == 0 ||
user.length == 0 || password.length == 0) {
NSLog(@"IGNORE AutoStart scrcpy!");
return;
}

[SSHParams setParamsWithServer:server port:port user:user password:password];
// reset error status
[[ExecStatus sharedStatus] resetStatus];

// Start scrcpy by detach from current stack
[self performSelector:@selector(scrcpyMain) withObject:nil afterDelay:0];
Expand Down
5 changes: 4 additions & 1 deletion scrcpy-fix/ssh.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <Foundation/Foundation.h>
#import <util/process.h>
#import "ExecStatus.h"

enum process_result ssh_exec(const char *const argv[]);
bool ssh_forward(const char *local_addr, const char *remote_addr);
Expand All @@ -18,11 +19,13 @@ bool ssh_reverse(uint16_t port);
@property (nonatomic, copy) NSString *sshUser;
@property (nonatomic, copy) NSString *sshPassword;

@property (nonatomic, copy) NSString *adbSerial;
@property (nonatomic, copy) NSString *scrcpyServer;

+(SSHParams *)sharedParams;
+(void)setParamsWithServer:(NSString *)server
port:(NSString *)port
user:(NSString *)user
password:(NSString *)password;
password:(NSString *)password
serial:(NSString *)serial;
@end
54 changes: 32 additions & 22 deletions scrcpy-fix/ssh.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#import "ssh.h"
#import <Gossh/Gossh.h>
#import "NSError+Alert.h"
#import "ExecStatus.h"
#import "config.h"

process_t adb_reverse_remove(const char *serial, const char *device_socket_name);

Expand All @@ -26,11 +26,14 @@ +(SSHParams *)sharedParams {
+(void)setParamsWithServer:(NSString *)server
port:(NSString *)port
user:(NSString *)user
password:(NSString *)password {
password:(NSString *)password
serial:(NSString *)serial
{
[self sharedParams].sshServer = server;
[self sharedParams].sshPort = port;
[self sharedParams].sshUser = user;
[self sharedParams].sshPassword = password;
[self sharedParams].adbSerial = serial;

[[self sharedParams] saveDefaults];
}
Expand All @@ -40,6 +43,7 @@ - (void)saveDefaults {
[[NSUserDefaults standardUserDefaults] setValue:self.sshPort forKey:@"ssh_port"];
[[NSUserDefaults standardUserDefaults] setValue:self.sshUser forKey:@"ssh_user"];
[[NSUserDefaults standardUserDefaults] setValue:self.sshPassword forKey:@"ssh_password"];
[[NSUserDefaults standardUserDefaults] setValue:self.adbSerial forKey:@"adb_serial"];
}

- (void)loadDefaults {
Expand All @@ -48,13 +52,15 @@ - (void)loadDefaults {
self.sshPort = (self.sshPort == nil || self.sshPort.length == 0) ? @"22" : self.sshPort;
self.sshUser = [[NSUserDefaults standardUserDefaults] valueForKey:@"ssh_user"];
self.sshPassword = [[NSUserDefaults standardUserDefaults] valueForKey:@"ssh_password"];
self.adbSerial = [[NSUserDefaults standardUserDefaults] valueForKey:@"adb_serial"];
}

- (NSString *)scrcpyServer {
if (_scrcpyServer.length > 0) {
return _scrcpyServer;
}
_scrcpyServer = @"/usr/local/share/scrcpy/scrcpy-server";
NSString *prefixDir = [NSString stringWithUTF8String:PREFIX];
_scrcpyServer = [prefixDir stringByAppendingString:@"/share/scrcpy/scrcpy-server"];
return _scrcpyServer;
}

Expand All @@ -72,16 +78,9 @@ - (NSString *)scrcpyServer {
return shell;
}

// reset error status
[[ExecStatus sharedStatus] resetStatus];

NSError *error;
SSHParams *sshParams = [SSHParams sharedParams];
[shell connect:sshParams.sshServer
port:sshParams.sshPort
user:sshParams.sshUser
password:sshParams.sshPassword
error:&error];
[shell connect:sshParams.sshServer port:sshParams.sshPort user:sshParams.sshUser password:sshParams.sshPassword error:&error];
if (error != nil) {
NSLog(@"Error: %@", error);
[error showAlert];
Expand All @@ -90,30 +89,40 @@ - (NSString *)scrcpyServer {
return shell;
}

NSError *errorAppendDesc(NSError *error, NSString *desc) {
NSString *newDesc = error.userInfo ? error.userInfo[NSLocalizedDescriptionKey] : @"";
newDesc = [newDesc stringByAppendingFormat:@"\n\n%@", desc];
return [NSError errorWithDomain:error.domain?:@"Scrcpy" code:error.code userInfo:@{
NSLocalizedDescriptionKey : newDesc,
}];
}

bool ssh_upload_scrcpyserver(void) {
NSString *scrcpyServer = [[NSBundle mainBundle] pathForResource:@"scrcpy-server" ofType:@""];
NSError *error = nil;
NSString *scrcpyDst = [SSHParams sharedParams].scrcpyServer;
BOOL success = [sshShell() uploadFile:scrcpyServer dst:scrcpyDst error:&error];
if (success == NO || error != nil) {
[error showAlert];
NSError *newErr = errorAppendDesc(error, @"Please check the PERMISSION of remote scrcpy-server path.");
[newErr showAlert];
return false;
}
NSLog(@"Upload scrcpy-server: %@", scrcpyDst);
NSLog(@"Uploaded: %@", scrcpyDst);
return true;
}

enum process_result ssh_exec_command(NSString *command) {
NSLog(@"Exec:\n%@", command);
NSError *error;
NSString *output = [sshShell() execute:command error:&error];
if (output != nil && output.length > 0) {
NSLog(@"Output:\n%@", output);
GosshShellStatus *status = [sshShell() execute:command];
if (status.output.length > 0) {
NSLog(@"Output:\n%@", status.output);
}
if (error != nil) {
NSLog(@"Error:\n%@", error);
[error showAlert];
[[ExecStatus sharedStatus] setError:error forCommand:command];
if (status.err != nil) {
NSLog(@"Error:\n%@", status.err);
NSError *newErr = errorAppendDesc(status.err, status.command);
newErr = errorAppendDesc(newErr, status.output);
[[ExecStatus sharedStatus] setError:newErr forCommand:command];
[newErr showAlert];
return PROCESS_ERROR_GENERIC;
}
return PROCESS_SUCCESS;
Expand Down Expand Up @@ -167,7 +176,8 @@ bool ssh_reverse(uint16_t port)
NSError *error = nil;

// force exit other clients first
NSString *killedPids = [sshShell() execute:@"pgrep -f \"scrcpy[-]server\" && kill `pgrep -f \"scrcpy[-]server\"`" error:&error];
GosshShellStatus *status = [sshShell() execute:@"pgrep -f \"scrcpy[-]server\" && kill `pgrep -f \"scrcpy[-]server\"`"];
NSString *killedPids = status.output;

// if command returns error, there's no other clients
// otherwise, we need wait a moment
Expand Down
26 changes: 21 additions & 5 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type Shell struct {
sshClient *ssh.Client
}

type ShellStatus struct {
Err error
Output string
Command string
}

func (s *Shell) Connect(host, port, user, password string) (err error) {
sshConfig := &ssh.ClientConfig{
Timeout: time.Second * 60,
Expand All @@ -40,17 +46,27 @@ func (s *Shell) Connected() bool {
return s.sshClient != nil
}

func (s *Shell) Execute(command string) (output string, err error) {
func (s *Shell) Execute(command string) *ShellStatus {
if s.sshClient == nil {
return "", errors.New("ssh is not connected")
return &ShellStatus {
Err: errors.New("ssh is not connected"),
Command: command,
}
}

sess, err := s.sshClient.NewSession()
if err != nil {
return "", err
return &ShellStatus{
Err: err,
Command: command,
}
}
out, err := sess.CombinedOutput("PATH=$PATH:/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin " + command)
return &ShellStatus{
Err: err,
Output: string(out),
Command: command,
}
out, err := sess.CombinedOutput("PATH=$PATH:/usr/local/bin:/usr/local/sbin " + command)
return string(out), err
}

func (s *Shell) Forward(localAddr, remoteAddr string) error {
Expand Down
2 changes: 1 addition & 1 deletion ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestShell_Execute(t *testing.T) {
shell := &Shell{ }
fmt.Println(shell.Connect("mbp.wsen.me", "22", "ethan", "aauu8800"))
fmt.Println(shell.Connect("wsen.me", "2022", "root", "root"))
fmt.Println(shell.Execute(`pgrep -f "scrcpy[-]server" && kill $(pgrep -f "scrcpy[-]server")`))
fmt.Println(shell.Execute(`export; which cat`))
}
Expand Down

0 comments on commit 823e2b1

Please sign in to comment.