• This repository has been archived on 25/Mar/2023
  • Stars
    star
    144
  • Rank 247,238 (Top 6 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drop-in embedded Telnet server for iOS and OS X apps

Overview

Version Platform License

GCDTelnetServer is a drop-in embedded Telnet server for iOS and OS X apps.

Features:

  • Elegant and simple API
  • Fully asynchronous (doesn't need the main thread)
  • Entirely built using Grand Central Dispatch for best performance and concurrency
  • Support for ANSI colors with an extension on NSMutableString
  • Can parse line inputs as command and arguments command line interface
  • Full support for IPv4 and IPv6
  • Automatically handles background and suspended modes on iOS
  • No dependencies on third-party source code
  • Available under a friendly New BSD License

Requirements:

  • OS X 10.8 or later (x86_64)
  • iOS 8.0 or later (armv7, armv7s or arm64)
  • ARC memory management only

Getting Started

Download or check out the latest release of GCDTelnetServer then add both the "GCDTelnetServer" and "GCDNetworking/GCDNetworking" subfolders to your Xcode project.

Alternatively, you can install GCDTelnetServer using CocoaPods by simply adding this line to your Xcode project's Podfile:

pod "GCDTelnetServer", "~> 1.0"

Using GCDTelnetServer in Your App

#import "GCDTelnetServer.h"

GCDTCPServer* server = [[GCDTelnetServer alloc] initWithPort:2323 startHandler:^NSString*(GCDTelnetConnection* connection) {
  
  // Return welcome message
  return [NSString stringWithFormat:@"You are connected using \"%@\"\n", connection.terminalType];
  
} lineHandler:^NSString*(GCDTelnetConnection* connection, NSString* line) {
  
  // Simply echo back the received line but you could do anything here
  return [line stringByAppendingString:@"\n"];
  
}];
[server start];

Then launch Terminal on your Mac, and simply enter telnet YOUR_COMPUTER_OR_IPHONE_IP_ADDRESS 2323 and voilà, you can communicate "live" with your app.

GCDTelnetServer has an extensive customization API, be sure to peruse GCDTelnetConnection.h.

Executing Remote Commands

The most interesting use of GCDTelnetServer is to execute commands inside your app while it's running on the device e.g. to query internal state, trigger actions while the app is in the background, etc...

This sample code demonstrate how to implement a welcome message with more information (and ANSI colors!), and also support 3 commands (quit, crash and setwcolor which takes some arguments):

#import "GCDTelnetServer.h"
#import "NSMutableString+ANSI.h"

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  GCDTCPServer* server = [[GCDTelnetServer alloc] initWithPort:2323 startHandler:^NSString*(GCDTelnetConnection* connection) {
    
    UIDevice* device = [UIDevice currentDevice];
    NSMutableString* welcome = [[NSMutableString alloc] init];
    [welcome appendANSIStringWithColor:kANSIColor_Green bold:NO format:@"You are connected from %@ using \"%@\"\n", connection.remoteIPAddress, connection.terminalType];
    [welcome appendANSIStringWithColor:kANSIColor_Green bold:NO format:@"Current device is %@ running %@ %@\n", device.model, device.systemName, device.systemVersion];
    return welcome;
    
  } commandHandler:^NSString*(GCDTelnetConnection* connection, NSString* command, NSArray* arguments) {
    
    if ([command isEqualToString:@"quit"]) {
      [connection close];
      return nil;
    } else if ([command isEqualToString:@"crash"]) {
      abort();
    } else if ([command isEqualToString:@"setwcolor"]) {
      if (arguments.count == 3) {
        dispatch_async(dispatch_get_main_queue(), ^{
          _window.backgroundColor = [UIColor colorWithRed:[arguments[0] doubleValue] green:[arguments[1] doubleValue] blue:[arguments[2] doubleValue] alpha:1.0];
        });
        return @"OK\n";
      }
      return @"Usage: setwcolor red green blue\n";
    }
    
    NSMutableString* error = [[NSMutableString alloc] init];
    [error appendANSIStringWithColor:kANSIColor_Red bold:YES format:@"UNKNOWN COMMAND = %@ (%@)\n", command, [arguments componentsJoinedByString:@", "]];
    return error;
    
  }];
  [server start];  // TODO: Handle error
  
  return YES;
}

And here's an example session:

$ telnet localhost 2323
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
You are connected from 127.0.0.1 using "XTERM-256COLOR"
Current device is iPad Simulator running iPhone OS 8.1
> test
UNKNOWN COMMAND = test ()
> setwcolor
Usage: setwcolor red green blue
> setwcolor 1 0 0
OK
> quitConnection closed by foreign host.

More Repositories

1

GCDWebServer

The #1 HTTP server for iOS, macOS & tvOS (also includes web based uploader & WebDAV server)
Objective-C
6,461
star
2

ComicFlow

Full source code for the ComicFlow comic reader for iPad
C++
359
star
3

XLFacility

Elegant and extensive logging facility for OS X & iOS (includes database, Telnet and HTTP servers)
Objective-C
315
star
4

Cooliris-ToolKit

Open source toolkit for iOS and OS X applications by Cooliris, Inc.
Objective-C
60
star
5

CompareFolders

Simple, fast and accurate folder comparison app for Mac
Objective-C
31
star
6

MidiHID

Allows you to use your favorite USB devices as MIDI controllers on Mac OS X
C
30
star
7

CodeMirrorView

Cocoa / Objective-C wrapper for CodeMirror
JavaScript
28
star
8

Libraries-OLD

Various precompiled open source libraries for OS X & iOS with build scripts
C++
22
star
9

Libraries

Precompiled libraries for OS X and iOS with their build scripts
C
16
star
10

SourceTextView

NSTextView subclass with basic syntax highlighting for C, C++, JavaScript and Lua
Objective-C
16
star
11

android-ndk-libraries

C
14
star
12

InAppStore

Reusable Obj-C class to handle App Store In-App Purchases and receipt validation
Objective-C
12
star
13

WaterRace

Full source code for 3D boat racing game released by French Touch early 2001
C++
11
star
14

Go-Bridge

How to cross-compile and call Go back and forth on OS X, iPhone & iOS Simulator
Shell
10
star
15

D3Visualizer

Experimental Mac IDE to build visualizations using D3.js, NVD3 and Crossfilter and SQLite data sources
Objective-C
8
star
16

netflow-prometheus-exporter

Go
6
star
17

GCDNetworking

Networking framework based on GCD
Objective-C
6
star
18

PolKit

My old Obj-C Toolkit
C
6
star
19

DirectoryScanner

Directory scanning and comparison Obj-C class
Objective-C
5
star
20

MovieScrapers

Python
4
star
21

PDF2CBZ

A command-line comic file converter from PDF to CBZ
C
4
star
22

MP3CDMaker

Mac app to convert iTunes playlists into MP3 CDs
Objective-C
4
star
23

Recipes

An experiment to store cooking recipes in GitHub
3
star
24

DatabaseStorage

Obj-C key-value store based on SQLite3
Objective-C
3
star
25

PolParser

Lightweight generic text parser in Obj-C
Objective-C
2
star
26

X-Tunes

Uninstrusive utility to control iTunes playback
Objective-C
2
star
27

XcodeAppTemplate

Template for OS X apps for the Mac App Store with IAP support
Objective-C
2
star
28

website

SCSS
1
star
29

Hold-Up

Full source code for the Mac accounting program Hold-Up (1999-2002)
C++
1
star
30

MovieConverter

Tool to batch convert folder of QuickTime compatible movies to 480p H264 / AAC
Objective-C
1
star
31

MixpanelTracker

Obj-C class to track events and user profiles with Mixpanel on OS X
Objective-C
1
star
32

rsync

Xcode project for rsync
Shell
1
star
33

TestNDK

Test Android app using the NDK with cURL and OpenSSL
C
1
star
34

Go-Bindings

Java
1
star
35

Mathusalem

Simple backup system for Mac OS X Leopard
Objective-C
1
star
36

Roku-Apps

Sample Roku apps
Brightscript
1
star